gpt4 book ai didi

Flutter:更改下拉菜单项选择列表的背景颜色

转载 作者:行者123 更新时间:2023-12-03 23:20:58 29 4
gpt4 key购买 nike

在以下示例中, DrowdownButton 包含带有白色文本的灰色背景(由容器框装饰定义)。因此,默认情况下,菜单项都具有白色文本。但是菜单选择列表包含白色背景,因此无法读取项目。有没有办法改变选择列表的背景?

[ DropDownButton ] 1 [ When clicked ] 2

@override
Widget build(BuildContext context) {
String dropdownValue = 'One';
return Scaffold(
body: Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(AppStyles.borderRadius),
color: Colors.grey,
),
padding: EdgeInsets.fromLTRB(8.0,0,8.0,0),
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward, color: Colors.white),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.white
),
underline: Container(
height: 0,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Three', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
),
),
),
);
}

最佳答案

嗨,请使用主题更改颜色。

import 'package:flutter/material.dart';

void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue.shade200,
),
child: new DropdownButton(
value: _value,
items: <DropdownMenuItem<int>>[
new DropdownMenuItem(
child: new Text('Hi'),
value: 0,
),
new DropdownMenuItem(
child: new Text('Hello'),
value: 1,
),
],
onChanged: (int value) {
setState(() {
_value = value;
});
},
),
),
),
);
}
}

关于Flutter:更改下拉菜单项选择列表的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58949698/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com