Flutter RadioListTile 改变 Radio(icon) 颜色
Flutter RadioListTile change Radio(icon) color
我无法更改 Radio List Tile 图标颜色。
我尝试使用 ListTileTheme 但没有用。
顺便说一句,我在对话框中使用 RadioListTile,但我认为这不会影响它。
代码。
ListTileTheme(
iconColor: AppColors.green,
textColor: AppColors.green,
child: SimpleDialog(
shape:
RoundedRectangleBorder(
borderRadius:
BorderRadius
.circular(10),
),
title: Text(
"Select Restaurant",
style: Theme.of(context)
.textTheme
.headline3,
textAlign:
TextAlign.center,
),
children: [
Divider(),
RadioListTile(
title: const Text(
'Name and Address'),
value: 1,
groupValue:
_isRadioSelected,
onChanged: (v) {
setState(() {
_isRadioSelected =
v;
});
},
),
],
),
);
您可以使用以下代码构建自定义的简单单选列表小部件。
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class GroupModel {
String text;
int index;
bool selected;
GroupModel({this.text, this.index, this.selected});
}
class MyApp extends StatefulWidget {
@override
_State createState() => new _State();
}
class _State extends State<MyApp> {
int _value2 = 0;
List<GroupModel> _group = [
GroupModel(text: "Item 1", index: 1, selected: true),
GroupModel(text: "Item 2", index: 2, selected: false),
GroupModel(text: "Item 3", index: 3, selected: false),
];
Widget makeRadioTileList() {
List<Widget> list = new List<Widget>();
for (int i = 0; i < _group.length; i++) {
list.add(new RadioListTile(
value: _group[i].index,
groupValue: _value2,
selected: _group[i].selected,
onChanged: (val) {
setState(() {
for (int i = 0; i < _group.length; i++) {
_group[i].selected = false;
}
_value2 = val;
_group[i].selected = true;
});
},
activeColor: Colors.purple,
controlAffinity: ListTileControlAffinity.trailing,
title: new Text(
' ${_group[i].text}',
style: TextStyle(
color: _group[i].selected ? Colors.black : Colors.grey,
fontWeight:
_group[i].selected ? FontWeight.bold : FontWeight.normal),
),
));
}
Column column = new Column(
children: list,
);
return column;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('RadioListTile Example'),
),
body: new Container(
padding: new EdgeInsets.all(30.0),
child: new Center(
child: new Column(
children: <Widget>[makeRadioTileList()],
),
),
),
);
}
}
Theme(
data: Theme.of(context).copyWith(
unselectedWidgetColor: Colors.green,
disabledColor: Colors.green
),
child: RadioListTile(
title: const Text('Name and Address'),
value: 1,
groupValue: _isRadioSelected,
onChanged: (v) {
setState(() {
_isRadioSelected = v;
});
},
),
)
使用主题更改当前小部件中的一些主题参数。
我无法更改 Radio List Tile 图标颜色。 我尝试使用 ListTileTheme 但没有用。 顺便说一句,我在对话框中使用 RadioListTile,但我认为这不会影响它。
代码。
ListTileTheme(
iconColor: AppColors.green,
textColor: AppColors.green,
child: SimpleDialog(
shape:
RoundedRectangleBorder(
borderRadius:
BorderRadius
.circular(10),
),
title: Text(
"Select Restaurant",
style: Theme.of(context)
.textTheme
.headline3,
textAlign:
TextAlign.center,
),
children: [
Divider(),
RadioListTile(
title: const Text(
'Name and Address'),
value: 1,
groupValue:
_isRadioSelected,
onChanged: (v) {
setState(() {
_isRadioSelected =
v;
});
},
),
],
),
);
您可以使用以下代码构建自定义的简单单选列表小部件。
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class GroupModel {
String text;
int index;
bool selected;
GroupModel({this.text, this.index, this.selected});
}
class MyApp extends StatefulWidget {
@override
_State createState() => new _State();
}
class _State extends State<MyApp> {
int _value2 = 0;
List<GroupModel> _group = [
GroupModel(text: "Item 1", index: 1, selected: true),
GroupModel(text: "Item 2", index: 2, selected: false),
GroupModel(text: "Item 3", index: 3, selected: false),
];
Widget makeRadioTileList() {
List<Widget> list = new List<Widget>();
for (int i = 0; i < _group.length; i++) {
list.add(new RadioListTile(
value: _group[i].index,
groupValue: _value2,
selected: _group[i].selected,
onChanged: (val) {
setState(() {
for (int i = 0; i < _group.length; i++) {
_group[i].selected = false;
}
_value2 = val;
_group[i].selected = true;
});
},
activeColor: Colors.purple,
controlAffinity: ListTileControlAffinity.trailing,
title: new Text(
' ${_group[i].text}',
style: TextStyle(
color: _group[i].selected ? Colors.black : Colors.grey,
fontWeight:
_group[i].selected ? FontWeight.bold : FontWeight.normal),
),
));
}
Column column = new Column(
children: list,
);
return column;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('RadioListTile Example'),
),
body: new Container(
padding: new EdgeInsets.all(30.0),
child: new Center(
child: new Column(
children: <Widget>[makeRadioTileList()],
),
),
),
);
}
}
Theme(
data: Theme.of(context).copyWith(
unselectedWidgetColor: Colors.green,
disabledColor: Colors.green
),
child: RadioListTile(
title: const Text('Name and Address'),
value: 1,
groupValue: _isRadioSelected,
onChanged: (v) {
setState(() {
_isRadioSelected = v;
});
},
),
)
使用主题更改当前小部件中的一些主题参数。