Flutter基础-048-CheckBoxListTile带有复选框的ListTile

342 阅读1分钟
构造方法
 const CheckboxListTile({
    Key key,
    @required this.value,    // 设定当前是否选中
    @required this.onChanged,   // 当勾选状态变化时回调此方法
    this.activeColor,// 选中后,复选框的颜色
    this.title,  // 主标题   第一行
    this.subtitle,  // 副标题   第二行
    this.isThreeLine = false,   // 是否空出第三行
    this.dense,
    this.secondary, // 左侧顶部的widget   一般是个icon,也可以定义成其他
    this.selected = false,// title subtitle secondary是否也采用activeColor的颜色,如果是true默认是activeColor的颜色,但优先级比较低,各widget也可以设定自己的颜色
    this.controlAffinity = ListTileControlAffinity.platform,
  })
示例

image.png

代码
class _MyHomePageState extends State<MyHomePage> {
  bool isSelected = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CheckboxListTile(
          value: isSelected,
          onChanged: (bool value){
            setState(() {
              isSelected = value;
            });
          },
          secondary: Icon(
            Icons.add_alarm,
//            color: Colors.orange[200],
          ),
          title: new Text("主标题"),
          subtitle: new Text("标题副s标题副s标题副s标题副s标题副s标题"),
          dense: false,
          activeColor: Colors.green[200],// 指定选中时勾选框的颜色
          isThreeLine: true,// 是否空出第三行
          selected: true,
        ),
      ),
    );
  }
}