单独一个复选框(CheckBox)
class CheckboxDemo extends StatefulWidget {
@override
_CheckboxDemoState createState() => _CheckboxDemoState();
}
class _CheckboxDemoState extends State<CheckboxDemo> {
bool _checkboxItemA = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('checkboxDemo'),
elevation: 0,
),
body: Container(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Checkbox(
value: _checkboxItemA,
onChanged: (value) {
setState(() {
_checkboxItemA = value;
});
},
activeColor: Colors.green, // 激活的颜色,默认为Theme的accentColor
),
],
)
],
),
),
);
}
}
预览
图标、文字和复选框(CheckBoxListTile)
class CheckboxDemo extends StatefulWidget {
@override
_CheckboxDemoState createState() => _CheckboxDemoState();
}
class _CheckboxDemoState extends State<CheckboxDemo> {
bool _checkboxItemA = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('checkboxDemo'),
elevation: 0,
),
body: Container(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CheckboxListTile(
value: _checkboxItemA,
onChanged: (value) {
setState(() {
_checkboxItemA = value;
});
},
title: Text('Checkbox Item A'),
subtitle: Text('Description'), // 副标题
secondary: Icon(Icons.bookmark),
selected: _checkboxItemA, // 设置图标和文字是否是激活状态,如果是true,则图标和文字均改变颜色
),
],
),
),
);
}
}
预览
网友评论