【问题标题】:Flutter button with icon and text looks odd带有图标和文本的颤振按钮看起来很奇怪
【发布时间】:2018-11-11 04:21:59
【问题描述】:

我正在尝试创建一个 Flutter 应用程序,该应用程序有一个带有文本和图标作为标签的按钮,图标放置在文本的右侧。 this post 中描述的方法会产生一个看起来很奇怪的按钮,该按钮似乎扩展到了应用程序的宽度,请参阅此图片(链接,因为我不允许附加图片):

Button becomes wide when adding a Row widget as child

我不清楚使用哪些布局小部件来调整 text+image 按钮以格式化为纯文本按钮。

产生上述例子的代码:

Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            new RaisedButton(
              onPressed: _incrementCounter,
              child: new Row(
                children: <Widget>[
                  new Text("Button with text and icon!"),
                  new Icon(Icons.lightbulb_outline)
                ],
              ),
            ),
            new RaisedButton(
              onPressed: _incrementCounter,
              child: new Text("Button with only text")
              ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    行属性 mainAxisSize: MainAxisSize.min 在这里可以解决问题。

    @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('Home'),
          ),
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new RaisedButton(
                  onPressed: () {},
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      new Text('Button with text and icon!'),
                      new Icon(Icons.lightbulb_outline),
                    ],
                  ),
                ),
                new RaisedButton(
                  onPressed: () {},
                  child: new Text('Button with only text'),
                )
              ],
            ),
          ),
        );
      }
    

    【讨论】:

      【解决方案2】:

      你可以简单地使用这个RaisedButton.icon

      简单用法:

      RaisedButton.icon(
         onPressed: () {},
         elevation: 2.0,
         shape: new RoundedRectangleBorder(
            borderRadius: new BorderRadius.circular(5.0),
         ),
         color: const Color(0xFFFFB822),
         icon: Icon(Icons.add_shopping_cart),
         label: Text("Add to Cart",
                   style: TextStyle(
                   fontWeight: FontWeight.w900,
          ),
        ),
      )
      

      【讨论】:

      • 如何为RaisedButton.icon 添加内边距或增大高度?
      • 如何添加自定义图标?假设我想要的图标在任何包中都不存在,我该如何添加添加我自己的?
      • @Suren 你应该检查一下fluttericon.com
      【解决方案3】:

      非常简单易行...

      ElevatedButton(
                      style: ElevatedButton.styleFrom(
                          primary: Colors.green, elevation: 2),
                      onPressed: () {},
                      child: Row(
                        children: [
                          Icon(Icons.edit_outlined),
                          SizedBox(
                            width: 10.0,
                          ),
                          Text('Edit'),
                        ],
                      ),
                    ),
      

      您可以随意更改图标或文本颜色。 希望对你有帮助?。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 2018-11-14
        • 1970-01-01
        相关资源
        最近更新 更多