您可以使用以下代码构建自定义的简单单选列表小部件。
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()],
),
),
),
);
}
}