Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何更改下拉按钮';什么是图标颜色?颤振_Flutter_Dropdown - Fatal编程技术网

Flutter 如何更改下拉按钮';什么是图标颜色?颤振

Flutter 如何更改下拉按钮';什么是图标颜色?颤振,flutter,dropdown,Flutter,Dropdown,在flatter中,我试图将下拉按钮图标(向下箭头图标)的颜色更改为白色 我尝试在没有帮助的情况下使用style属性。文本颜色变为白色,但图标仍为默认灰色 DropdownButton( style: TextStyle(color: Colors.white, decorationColor: Colors.white), items: this.items, value: null, hint

在flatter中,我试图将下拉按钮图标(向下箭头图标)的颜色更改为白色

我尝试在没有帮助的情况下使用style属性。文本颜色变为白色,但图标仍为默认灰色

DropdownButton(
         style: TextStyle(color: Colors.white, decorationColor: 
             Colors.white),
         items: this.items,
         value: null,
         hint: Text(SaveOptions[_saveOption], style: TextStyle(color: 
             Colors.white)),
         onChanged: (selectedOption) {
           setState(() {
             _saveOption = selectedOption;
           });
         })

如何将箭头图标的颜色更改为白色?

目前,箭头颜色已硬编码为
下拉按钮

  Color get _downArrowColor {
    // These colors are not defined in the Material Design spec.
    if (_enabled) {
      if (Theme.of(context).brightness == Brightness.light) {
        return Colors.grey.shade700;
      } else {
        return Colors.white70;
      }
    } else {
      if (Theme.of(context).brightness == Brightness.light) {
        return Colors.grey.shade400;
      } else {
        return Colors.white10;
      }
    }
  }

您可以创建自己的小部件来自定义此属性。

由于
下拉按钮
从最近的
主题
获取颜色,因此您有两个选项

第一种是通过改变应用程序主题的亮度

另一种方法是用新的
主题
下拉列表
按钮包装成暗的
亮度

Theme(
   data: Theme.of(context).copyWith(brightness: Brightness.dark),
   child: DropdownButton(
     style: TextStyle(color: Colors.white, decorationColor: Colors.white),
     items: this.items,
     value: null,
     hint: Text(SaveOptions[_saveOption], style: TextStyle(color: Colors.white)),
     onChanged: (selectedOption) {
       setState(() {
         _saveOption = selectedOption;
       });
     },
   ),
 )

看来弗利特应该有办法做到这一点,但我认为目前这是不可能的。我所做的是将“value”设置为null,“iconSize”设置为0,并根据所选内容动态生成“hint”。这样做可以让您完全控制提示小部件

DropdownButton<int>(
  value: null,
  iconSize: 0,
  hint: Row(
    children: <Widget>[
      Text(_selected,
        style: TextStyle(
          color: Colors.white,
          fontWeight: FontWeight.w700,
        ),
      ),
      Padding(
        padding: EdgeInsets.only(left: 5),
        child: Icon(
          FontAwesomeIcons.caretDown,
          color: Colors.white,
          size: 20,
        ),
      ),
    ],
  ),
  items: dateRanges.map((Map<String, dynamic> value) {
    return DropdownMenuItem<int>(
      value: value['type'],
      child: Text(
        value['name'],
        style: TextStyle(
          color: Colors.grey[800],
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }).toList(),
  onChanged: (type) => _onDateRangeTypeChanged(type),
)
下拉按钮(
值:null,
iconSize:0,
提示:行(
儿童:[
文本(_)已选定,
样式:TextStyle(
颜色:颜色,白色,
fontWeight:fontWeight.w700,
),
),
填充物(
填充:仅限边缘设置(左:5),
子:图标(
FontAwesomeIcons.caretDown,
颜色:颜色,白色,
尺码:20,
),
),
],
),
项目:dateRanges.map((映射值){
返回下拉菜单项(
值:值['type'],
子:文本(
值['name'],
样式:TextStyle(
颜色:颜色。灰色[800],
fontWeight:fontWeight.w700,
),
),
);
}).toList(),
一旦更改:(类型)=>\u OnDaterRangeTypeChanged(类型),
)

希望这能有所帮助。

这有点麻烦,但它可以让您完全控制下拉列表的收拢方式,简言之,make value:null,hint:null,iconsize:null,制作一个包含两个大小相同的容器的堆栈:1个显示收拢的下拉列表,1个检测手势“展开”

class MyDropdownFilled extends StatefulWidget {
  final List<String> dropDownValues;

  const MyDropdownFilled({Key key, @required this.dropDownValues})
      : super(key: key);

  List<DropdownMenuItem<String>> getDropDownMenuItems() {
    return dropDownValues
        .map((itemString) =>
            DropdownMenuItem(child: Text(itemString), value: itemString))
        .toList();
  }

  @override
  _MyDropdownFilledState createState() => _MyDropdownFilledState();
}

class _MyDropdownFilledState extends State<MyDropdownFilled> {
  String _activeDropdown;

  @override
  initState() {
    super.initState();
    _activeDropdown = widget.dropDownValues[0];
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              color: primaryColor.shade600,
              borderRadius: BorderRadius.all(Radius.circular(2))),
          child:
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Text(_activeDropdown, style: Theme.of(context).textTheme.caption),
            Icon(Icons.arrow_drop_down, size: 30, color: Colors.white),
          ]),
        ),
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              color: Colors.transparent,
              borderRadius: BorderRadius.all(Radius.circular(2))),
          child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
            value: null,
            isDense: true,
            iconSize: 0,
            hint: null,
            onChanged: (String newValue) {
              setState(() {
                _activeDropdown = newValue;
              });
            },
            items: widget.dropDownValues.map((String value) {
              return DropdownMenuItem(
                value: value,
                child: Text(value),
              );
            }).toList(),
          )),
        )
      ],
    );
  }
}
class MyDropdownFilled扩展了StatefulWidget{
最终列表下拉列表值;
常量MyDropdownFilled({Key Key,@required this.dropDownValues})
:super(key:key);
列表getDropDownMenuItems(){
返回下拉列表值
.map((itemString)=>
DropdownMenuItem(子项:文本(itemString),值:itemString))
.toList();
}
@凌驾
_MyDropdownFilledState createState()=>\u MyDropdownFilledState();
}
类_MyDropDownFilled状态扩展状态{
字符串_active下拉列表;
@凌驾
initState(){
super.initState();
_activeDropdown=widget.dropDownValues[0];
}
@凌驾
小部件构建(构建上下文){
返回堆栈(
儿童:[
容器(
宽度:double.infinity,
填充:所有边缘设置(10.0),
装饰:盒子装饰(
颜色:primaryColor.shade600,
borderRadius:borderRadius.all(半径.圆形(2)),
儿童:
行(mainAxisAlignment:mainAxisAlignment.spaceBetween,子项:[
Text(_activeDropdown,style:Theme.of(context.textTheme.caption),
图标(Icons.arrow\u下拉,大小:30,颜色:Colors.white),
]),
),
容器(
宽度:double.infinity,
填充:所有边缘设置(10.0),
装饰:盒子装饰(
颜色:颜色。透明,
borderRadius:borderRadius.all(半径.圆形(2)),
子项:DropdownButtonHideUnderline(
孩子:下拉按钮(
值:null,
是的,
iconSize:0,
提示:空,
onChanged:(字符串newValue){
设置状态(){
_activeDropdown=newValue;
});
},
条目:widget.dropDownValues.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
)),
)
],
);
}
}

转到下拉按钮类 编辑此代码

if (!DropdownButtonHideUnderline.at(context)) {
      final double bottom = widget.isDense ? 0.0 : 8.0;
      result = Stack(
        children: <Widget>[
          result,
          Positioned(
            left: 0.0,
            right: 0.0,
            bottom: bottom,
            child: Container(
              height: 1.0,
              decoration: const BoxDecoration(
                border: Border(bottom: BorderSide(color: Color(0xFFBDBDBD), width: 0.0))
              ),
            ),
          ),
        ],
      );
    }

您可以按以下方式使用字段
iconEnabledColor
iconDisabledColor

final myDropDownMenu = DropdownButton<String>(
      iconEnabledColor: Colors.white,
      iconDisabledColor: Colors.white,
      value: myInitialValue,
      // The rest of your code
);
final myDropDownMenu=下拉按钮(
iconEnabledColor:Colors.white,
iconDisabledColor:Colors.white,
值:myInitialValue,
//代码的其余部分
);

将您的小部件包装在一个新主题周围,该主题具有您想要设置的值,因为您可以转到源代码,查看它在主题之外使用的颜色

, width: 0.0))
                  ),
                ),
              ),
            ],
          );
        }
final myDropDownMenu = DropdownButton<String>(
      iconEnabledColor: Colors.white,
      iconDisabledColor: Colors.white,
      value: myInitialValue,
      // The rest of your code
);