Android TextView的线条相当于颤动

时间:2018-03-15 17:53:00

标签: android text textview flutter

所以,我需要制作一个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))
          ])
 )

enter image description here

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

enter image description here

那么,无论用户的设备文字大小如何,任何想法如何在flutter android:Lines窗口小部件中复制Text行为?

更新:为更好的上下文添加了全面的代码

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)),
                                  ))
                            ],
                          ),
                        ],
                      ))),
            ],
          ),
        ));
  }
}

1 个答案:

答案 0 :(得分:1)

问题是您要为列设置有限高度。并且该高度不足以显示具有此文本样式的2行。

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