Flutter 2:带有图标和文本的按钮

st3*_*fb3 7 dart flutter

由于RaisedButtonFlatButton等已被弃用,是否有任何按钮小部件可以提供文本和图标以防止行创建或其他解决方法?

ישו*_*ותך 17

您可以使用ElevatedButton.icon

这里是示例按钮:

在此输入图像描述

代码:

 // Color state for button.
 Color _getTextColor(Set<MaterialState> states) => states.any(<MaterialState>{
    MaterialState.pressed,
    MaterialState.hovered,
    MaterialState.focused,
  }.contains)
      ? Colors.green
      : Colors.blue;

  Widget _myButton() {
     return ElevatedButton.icon(
        onPressed: () {/* do something here */ },
        icon: Icon(Icons.edit),
        style: ButtonStyle(backgroundColor: MaterialStateColor.resolveWith(_getTextColor)),
        label: Text(
          "Update",
          style: TextStyle(color: Colors.white),
        ));
  }
Run Code Online (Sandbox Code Playgroud)


Kis*_*cha 5

您可以简单地使用TextButton.icon()构造函数。

TextButton.icon(
    icon: Icon(), // Your icon here
    label: Text(), // Your text here
    onPressed: (){},
)
Run Code Online (Sandbox Code Playgroud)

有关 TextButton.icon() 构造函数的更多信息,您可以访问此站点


小智 5

你可以像这样添加 TextButton.icon

TextButton.icon(
                icon: Icon(Icons.photo, color:Colors.black ),
                label: Text(
                  "Gallery",
                  style: TextStyle(color: Colors.black),
                ),
                onPressed: () {
                  // call method

                },

            ),
Run Code Online (Sandbox Code Playgroud)