【问题标题】:Adding icon to left of DropDownButton (expanded) in flutter在颤动中将图标添加到 DropDownButton(扩展)的左侧
【发布时间】:2020-03-12 19:14:45
【问题描述】:

我想在DropDownButton 的左侧添加图标,但找不到这样做的方法。 我想要达到的效果是这样的:

我尝试了以下代码,但它会将图标放在我不想要的位置,并且它还覆盖了箭头图标:

 `@override
  Widget build(BuildContext context) {
  return Scaffold(
  body: Container(
    margin: EdgeInsets.only(top: 64.0, left: 16.0, right: 16.0),
    color: Colors.white,
    child: DropdownButton(
      icon: Icon(
        Icons.person,
        color: Colors.redAccent,
        size: 20.09,
      ),
      isExpanded: true,
      items: _studentList.map((val) {
        return DropdownMenuItem(
          value: val,
          child: Text(val),
        );
      }).toList(),
      value: _currentSelectedItem,
      onChanged: (value) {
        setState(() {
          _currentSelectedItem = value;
        });
      },
    ),
  ),
);
  }`

上面代码的输出是这样的:

我还尝试将Icon()DropDownButton() 放在Row() 小部件中,但这不允许DropDownButton() 扩展到全宽。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: android flutter dart material-design dropdown


    【解决方案1】:

    使用 DropdownButtonFormField 因为它具有装饰属性。您可以使用 prefixIcon 属性在左侧设置图标。

    示例:

    DropdownButtonFormField<String>(
             decoration: InputDecoration(
             prefixIcon: Icon(Icons.person),
             ),
             hint: Text('Please choose account type'),
             items: <String>['A', 'B', 'C', 'D'].map((String value) {
             return DropdownMenuItem<String>(
             value: value,
             child: new Text(value),
           );
          }).toList(),
         onChanged: (_) {},
    ),
    

    结果:

    【讨论】:

      【解决方案2】:

      这可能为时已晚。但对即将到来的观众会有帮助。

      我们可以使用 DropDownButtonFormField,而不是使用 DropDownButton。其中有一个 decoration 属性。这样我们就可以利用 prefixIcon 属性在 DropDownButton 的左侧添加图标:

      DropDownButtonFormField(
        decoration: InputDecoration(
          prefixIcon: Icon(Icons.person, color: Colors.redAccent),
        ),
        ...
      );
      

      【讨论】:

        【解决方案3】:

        执行此操作的简单方法是将子道具传递给 DropdownMenuItem,然后您可以传递任何小部件,我创建了一行并传递了该行中的图标和文本:

        items: <String>['George Green School', 'George Blue School', 'George Red School', 'George Yellow School']
                        .map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Row(
                          children: [
                            Icon(Icons.dangerous),
                            Text(value, style: TextStyle(fontSize : 12),),
                          ],
                        ),
                      );
                    }).toList(),
        

        【讨论】:

        • 最佳答案是将下拉菜单和图标包装在 Row() 小部件中。
        • 不确定你的意思,将一个图标和整个下拉列表包装成一行,将创建一个图标和一个下拉菜单,而不是每个下拉项目一个图标。
        • 没错,这就是我想要达到的目标,那时我还是个初学者,所以遇到了很多问题,但无论如何,感谢您的评论
        【解决方案4】:

        将您的图标和下拉小部件包装在一个行小部件内,嵌套在一个容器内将执行如下工作:

        body: Container(
            margin: EdgeInsets.only(top: 64.0, left: 16.0, right: 16.0),
            color: Colors.white,
            child: 
            child: Row(
            children: <Widget>[
              Icon(
                Icons.person,
                color: Colors.redAccent,
                size: 20.09,
             ),
             Expanded(
                flex:1,
                child:DropdownButton(
                    isExpanded: true,
                    items: _studentList.map((val) {
                    return DropdownMenuItem(
                      value: val,
                      child: Text(val),
                    );
                }).toList(),
                value: _currentSelectedItem,
                onChanged: (value) {
                 setState(() {
                   _currentSelectedItem = value;
                 });
              },
             ),
            )
          ]  
        )
        

        【讨论】:

        • 代码格式应正确。括号目前也不匹配
        【解决方案5】:

        记住,一切都是小部件,所以:

        iconSize: 0,
        hint: Row(
                 children: [
                    Container(
                        child: Text('Freguesias'),
                    ),
                    Container(
                        child: Icon(Icons.arrow_drop_down),
                    ),
                  ],
         ),
        

        【讨论】:

          【解决方案6】:

          没有用于以您想要的方式添加图标的特定属性,但您始终可以解决您的代码并找到一些调整。使用以下代码将您的Icon() 置于您的DropDownButton() 按钮之上。

          @override
          Widget build(BuildContext context) {
          return Scaffold(
          body: Stack(
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey,
                      blurRadius: 20.0,
                      spreadRadius: 0.5,
                      offset: Offset(1.0, 1.0),
                    ),
                  ],
                ),
                padding: EdgeInsets.only(left: 44.0),
                margin: EdgeInsets.only(top: 64.0, left: 16.0, right: 16.0),
                child: DropdownButton(
                  isExpanded: true,
                  items: your_list.map(
                    (val) {
                      return DropdownMenuItem(
                        value: val,
                        child: Text(val),
                      );
                    },
                  ).toList(),
                  value: _currentSelectedItem,
                  onChanged: (value) {
                    setState(() {
                      _currentSelectedItem = value;
                    });
                  },
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 80.0, left: 28.0),
                child: Icon(
                  Icons.person,
                  color: Colors.redAccent,
                  size: 20.0,
                ),
              ),
            ],
          ),
            );
          }
          

          【讨论】:

          • 感谢 Sameed 的回答,我会检查并尽快回复您
          • @Sameed Shah 如何在点击图标时调用相同的下拉菜单
          • 这个答案不能被接受,因为箭头点击下拉按钮没有打开
          【解决方案7】:

          如果图标确实应该是 DropDown 组件的视觉部分,您可以采用以下路线。它有点 hacky 并且缺乏响应性,但它​​是更多实验的开始。我添加了一个完整的工作示例,因此您可以将其复制粘贴到可运行的主 dart 文件中。

          简而言之,它使用堆栈布局,因此图标只是放置在下拉组件上。然后我填充了文本,以便左侧有空间。

          void main() => runApp(MyApp());
          
          /// This Widget is the main application widget.
          class MyApp extends StatelessWidget {
            static const String _title = 'Flutter Code Sample';
          
            @override
            Widget build(BuildContext context) {
              return MaterialApp(
                title: _title,
                home: Scaffold(
                  appBar: AppBar(title: const Text(_title)),
                  body: MyStatefulWidget(),
                ),
              );
            }
          }
          
          class MyStatefulWidget extends StatefulWidget {
            MyStatefulWidget({Key key}) : super(key: key);
          
            @override
            _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
          }
          
          class _MyStatefulWidgetState extends State<MyStatefulWidget> {
            String dropdownValue = 'One';
          
            @override
            Widget build(BuildContext context) {
              return Scaffold(
                body: Center(
                  child: Stack(
                    children: <Widget>[
                      DropdownButton<String>(
                        value: dropdownValue,
                        //icon: Icon(Icons.arrow_downward),
                        iconSize: 24,
                        elevation: 16,
                        style: TextStyle(color: Colors.deepPurple),
                        underline: Container(
                          height: 2,
                          color: Colors.deepPurpleAccent,
                        ),
                        onChanged: (String newValue) {
                          setState(() {
                            dropdownValue = newValue;
                          });
                        },
                        items: <String>['One', 'Two', 'Free', 'Four']
                            .map<DropdownMenuItem<String>>((String value) {
                          return DropdownMenuItem<String>(
                            value: value,
                            child: Padding(
                              padding: const EdgeInsets.only(left: 30.0),
                              child: Text("$value"),
                            ),
                          );
                        }).toList(),
                      ),
                      Container(
                          margin: EdgeInsets.symmetric(vertical: 10),
                          child: Icon(Icons.arrow_downward)),
                    ],
                  ),
                ),
              );
            }
          }
          

          【讨论】:

          • 对不起,迟到的评论,我会尽快给你我的反馈
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-20
          • 2012-12-05
          • 2015-11-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多