【问题标题】:Android TextView's Lines equivalent in flutterAndroid TextView 的 Lines 等效于颤动
【发布时间】:2018-03-15 17:52:59
【问题描述】:

所以,我需要制作一个Text 小部件来获得准确的行数。在android studio中,我只需要添加属性android:Lines = 2然后TextView将是2行,无论文本有多长(如果少于2行,则用空格填充第二行)并且不管用户设备的字体大小。

问题是,我很难动态地在颤动中复制这种行为。下面的屏幕截图仅适用于特定用户设备的 fontSize,因为它使用精确的高度 (32.0)。

new Container(
     height: 32.0,
     child: new Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
               new Expanded(
                    child: new Text('Curry Rice'), maxLines: 2, overflow: TextOverflow.ellipsis))
          ])
 )

当用户增加/减少他们设备的字体大小时会出现问题,这将使固定高度的容器不再工作。

那么,无论用户的设备文本大小如何,知道如何在颤振 Text 小部件中复制 android:Lines 行为吗?

更新:为更好的上下文添加了综合代码

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // final wordPair = new WordPair.random();
    return new MaterialApp(
        title: 'App Title',
        theme:
            new ThemeData(primaryColor: Colors.white, accentColor: Colors.blue),
        home: new Scaffold(
            appBar: new AppBar(
              title: new Text('App Title'),
              actions: <Widget>[
                new IconButton(icon: new Icon(Icons.list), onPressed: null),
              ],
            ),
            body: new Center(
                child: new Padding(
                    padding: new EdgeInsets.all(16.0),
                    child: new TestWidget()))));
  }
}

class TestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new SizedBox(
        width: 150.0,
        child: new Card(
          child: new Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new Image.asset(
                'assets/thumb2.jpg',
                color: Colors.red,
              ),
              new InkWell(
                  onTap: () {
                    // _showMenuDescription(context);
                  },
                  child: new Padding(
                      padding: new EdgeInsets.all(8.0),
                      child: new Column(
                        children: <Widget>[
                          new Container(
                            height: 32.0,
                            child: new Column(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: <Widget>[
                                  new Expanded(
                                    child: new Text(
                                        'Curry Rice With Tandoori Chicken and Sunny Side Fried Egg',
                                        maxLines: 2,
                                        overflow: TextOverflow.ellipsis),
                                  )
                                ]),
                          ),
                          new Container(height: 8.0),
                          new Row(
                            children: <Widget>[
                              new Expanded(
                                child: new Text(
                                  'BBQ Sauce',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: new TextStyle(color: Colors.black45),
                                ),
                              ),
                              new Padding(
                                  padding: new EdgeInsets.only(left: 8.0),
                                  child: new Text(
                                    '20,000',
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    textAlign: TextAlign.end,
                                    style: new TextStyle(
                                        color: const Color(0xffffa000)),
                                  ))
                            ],
                          ),
                        ],
                      ))),
            ],
          ),
        ));
  }
}

【问题讨论】:

  • 你有什么解决办法吗? ,请告诉我
  • @RavindraKushwaha 事实证明这是不可能的。因为没有 minLines 属性。我认为您需要使用MediaQuery 或其他方式手动计算。

标签: android text textview flutter


【解决方案1】:

问题是您为列设置了有限的高度。而且这个高度还不足以显示 2 行这种文字样式。

增加列高或完全删除该约束应该可以解决问题。

【讨论】:

  • 当然还不够。正如我所说,因为我不能让它“无论设备的文本大小比例是 2 行”。为此,我需要将高度值设置为动态的,这是我提出的问题。
  • 如果我增加列高,当用户的设备文本尺寸较小时,与文本相比,空白空间变得太大。如果我删除初始高度,当只有 1 行文本时,第二行的空间将不存在
  • 认为它就像android:Lines 属性。无论大小如何,它都会将文本设为 2 行。
  • 删除 height 使其动态化。
  • 删除它会引发异常,因为我将它放在列中。请看看我的更新。我放了一个更全面的代码,而不仅仅是一个简短的sn-p。谢谢。
猜你喜欢
  • 2021-05-02
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
相关资源
最近更新 更多