Flutter开发之——交互组件-Checkbox和CheckboxListTile
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Checkbox List Tile Example'),
),
body: CheckboxListTileExample(),
),
);
}
}
class CheckboxListTileExample extends StatefulWidget {
@override
_CheckboxListTileExampleState createState() => _CheckboxListTileExampleState();
}
class _CheckboxListTileExampleState extends State<CheckboxListTileExample> {
bool isSelected = false;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
CheckboxListTile(
title: Text('I like Flutter'),
value: isSelected,
onChanged: (bool value) {
setState(() {
isSelected = value;
});
},
),
SizedBox(height: 20.0),
Text('Checkbox is ${isSelected ? 'checked' : 'unchecked'}'),
],
);
}
}
这段代码创建了一个带有CheckboxListTile的页面,用户可以通过点击Checkbox来改变其选中状态。CheckboxListTile同时显示了标题和描述文本,并在用户交互时更新状态。这是一个简单的状态管理示例,适合初学者学习和理解CheckboxListTile的工作原理。
评论已关闭