【问题标题】:How to add image/icon for right side of a button?如何为按钮右侧添加图像/图标?
【发布时间】:2019-10-16 18:26:03
【问题描述】:

这些天我正在为 Android 平台开发 Flutter 移动应用程序。我想添加一个带有文本图标/图像的按钮。该图像必须是按钮文本的右侧。

我已经在此处附上了图片。

这是我的代码。

child: FlatButton.icon(
     icon: Image.asset("images/notesicon.png", width: 20.0,height: 20.0,),
     label: Text("Add Note",
        style: TextStyle(
           fontSize: 11.0,
           fontFamily: "Raleway"
        ),
     ),
     textColor: Colors.white,
     color: Color(0xFF226597),
     shape: OutlineInputBorder(borderSide: BorderSide(
               style: BorderStyle.solid,
               width: 1.0,
               color: Colors.black),
            borderRadius: new BorderRadius.circular(20.0)
      ),
    ),

【问题讨论】:

  • 所以,向我们展示你到目前为止的尝试
  • 我使用了FlatButton.icon。但这里的图像只出现在右侧。
  • 图标后显示文本:FlatButton.icon(icon: Icon(Icons.call), label: Text('Call Me', style: Theme.of(context).textTheme.headline3, ), onPressed: () {}, ),

标签: flutter flutter-layout flutter-widget


【解决方案1】:

试试下面的代码:

FlatButton(
   padding: EdgeInsets.all(10.0),
   child: Row(
   children: < Widget > [
     Text("Make a Note"),
     Icon(Icons.note),
   ],
 ),
)

【讨论】:

    【解决方案2】:

    在这里你已经修复了你的代码,不要使用FlatButton.icon,只需使用FlatButton 构造函数并添加一个自定义子级,在这种情况下Row

        SizedBox(
                width: 150,
                child: FlatButton(
                  child: Row(
                    children: [
                      Text(
                        "Add Note",
                        style: TextStyle(fontSize: 11.0, fontFamily: "Raleway"),
                      ),
                      Image.asset(
                        "images/notesicon.png",
                        width: 20.0,
                        height: 20.0,
                      )
                    ],
                  ),
                  onPressed: () {},
                  textColor: Colors.white,
                  color: Color(0xFF226597),
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(
                          style: BorderStyle.solid, width: 1.0, color: Colors.black),
                      borderRadius: new BorderRadius.circular(20.0)),
                ),
              ), 
    

    【讨论】:

    • 当使用Row 作为FlatButton 的子级时,按钮总是试图水平扩展,这可能很烦人。根据FlatButton.icon源代码,您可以在Row中使用mainAxisSize: MainAxisSize.min来防止其扩展。
    【解决方案3】:

    你可以这样做

     Container(
                    width: 150,
                    height: 100,
                        padding: EdgeInsets.all(10),
                        decoration: new BoxDecoration(
                          color: Colors.blue[400],
                          border: Border.all(color: Colors.blue[800], width: 4.0),
                          borderRadius:
                          new BorderRadius.all(Radius.circular(40.0)),
                        ),
                        child:Center(child: FlatButton(
                          onPressed: () => {},
                          padding: EdgeInsets.all(5.0),
                          child:Center(child:  Row(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: <Widget>[
                              Text("Make A Note"),
                              Icon(Icons.note_add),
                            ],
                          ),),),)),
    

    【讨论】:

      【解决方案4】:

      您可以使用此小部件创建一个带有操作的独立按钮。以及一些必要的 UI 更改。

            Widget btnChooseFromDevice() {
              return Container(
                height: 56.0,
                width: MediaQuery.of(context).size.width,
                child: FlatButton(
                  child: Row(
                    children: <Widget>[
                      SizedBox(width: 20.0,),
                      Text(
                        'Choose from Device',
                        style: TextStyle(
                          fontSize: 20.0,
                          fontFamily: 'Righteous',
                          fontWeight: FontWeight.bold,
                        ),
                       Container(
                        width: 36,
                        height: 36,
                        child: Image.asset("assets/images/file.png"),
                      ),
                      ),
      
                    ],
                  ),
                  textColor: Colors.white,
                  shape:
                  RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
                  onPressed: () {
                    print('button pressed');
          //          getImage();
                  },
                ),
              );
            }
      

      【讨论】:

        【解决方案5】:

        只需翻转它们。 (lableicon) 因为它们都接受任何小部件。

        child: FlatButton.icon(
         icon: Text("Add Note",
            style: TextStyle(
               fontSize: 11.0,
               fontFamily: "Raleway"
            ),
         ),
         label: Image.asset("images/notesicon.png", width: 20.0,height: 20.0,),
         textColor: Colors.white,
         color: Color(0xFF226597),
         shape: OutlineInputBorder(borderSide: BorderSide(
                   style: BorderStyle.solid,
                   width: 1.0,
                   color: Colors.black),
                borderRadius: new BorderRadius.circular(20.0)
          ),
        ),
        

        【讨论】:

        • 智能解决方案!
        • 这么聪明的解决方案!
        • 哇,谢谢,交换图标和文字的方向真的很管用...
        • 因跳出框框思考而被投票!
        • 此解决方案有效,但请注意文本可能不再正确换行。 (我正在使用非常大的辅助功能字体大小测试我的应用程序,文本被截断,而不是换行。将文本放回 label 使文本再次正确换行。)
        【解决方案6】:
        child: FlatButton.icon(
         icon:  Text("Add Note",
            style: TextStyle(
               fontSize: 11.0,
               fontFamily: "Raleway"
            ),
         ),
         label: Image.asset("images/notesicon.png", width: 20.0,height: 20.0,),
         textColor: Colors.white,
         color: Color(0xFF226597),
         shape: OutlineInputBorder(borderSide: BorderSide(
                   style: BorderStyle.solid,
                   width: 1.0,
                   color: Colors.black),
                borderRadius: new BorderRadius.circular(20.0)
          ),
        ),
        

        【讨论】:

          【解决方案7】:

          你可以试试这个。

          FlatButton(
              padding: EdgeInsets.fromLRTB(8.0, 8.0, 8.0, 8.0),
                  child: Row(
                      children: < Widget > [
                      Text("Add a Note"),
                      Icon(Icons.note_add),
                   ],
              ),
          )
          

          【讨论】:

            【解决方案8】:
            Container(
                    decoration: new BoxDecoration(
                      color: Colors.white,
                      border: Border.all(color: Colors.blue, width: 1.0),
                      borderRadius: new BorderRadius.all(Radius.circular(3.0)),
                    ),
                    width: double.infinity,
                    child: FlatButton(
                      padding: EdgeInsets.all(8.0),
                      onPressed: () {},
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text("Add a Note"),
                          Icon(
                            Icons.note_add,
                            color: Colors.blue,
                          ),
                        ],
                      ),
                    ),
                  ),[If you want Flatbutton like this you can use this following code][1]
            

            【讨论】:

            • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-25
            • 1970-01-01
            • 2017-01-25
            • 1970-01-01
            • 1970-01-01
            • 2021-03-14
            相关资源
            最近更新 更多