Flutter开发之——交互组件-Checkbox和CheckboxListTile

一 概述

本文介绍Flutter中的复选框组件

  • Checkbox是只有复选框的组件
  • CheckboxListTile是带有复选框和标题文字的组件

二 Checkbox

2.1 构造方法

 const Checkbox({
    Key? key,
    required this.value,
    this.tristate = false,
    required this.onChanged,
    this.mouseCursor,
    this.activeColor,
    this.fillColor,
    this.checkColor,
    this.focusColor,
    this.hoverColor,
    this.overlayColor,
    this.splashRadius,
    this.materialTapTargetSize,
    this.visualDensity,
    this.focusNode,
    this.autofocus = false,
  }) : assert(tristate != null),
       assert(tristate || value != null),
       assert(autofocus != null),
       super(key: key);

2.2 常用属性

属性说明取值
value是否被选中bool对象
onChanged选中变化ValueChanged<bool?>?对象
activeColor复选框背景颜色Colors对象
checkColor选中对号颜色Colors对象

2.3 示例

代码
var _checkValue = false;
Checkbox(
          activeColor: Colors.grey,
          checkColor: Colors.red,
          value: _checkValue,
          onChanged: (value) {
              setState(() {
                   _checkValue = value;
              });
        })
效果图

三 CheckboxListTile

3.1 构造方法

const CheckboxListTile({
    Key? key,
    required this.value,
    required this.onChanged,
    this.activeColor,
    this.checkColor,
    this.tileColor,
    this.title,
    this.subtitle,
    this.isThreeLine = false,
    this.dense,
    this.secondary,
    this.selected = false,
    this.controlAffinity = ListTileControlAffinity.platform,
    this.autofocus = false,
    this.contentPadding,
    this.tristate = false,
    this.shape,
    this.selectedTileColor,
  }) 

3.2 常用属性

属性说明取值
value是否被选中bool对象
onChanged选中变化ValueChanged<bool?>?对象
activeColor复选框背景颜色Colors对象
checkColor选中对号颜色Colors对象
title标题Widget对象
subtitle副标题Widget对象
secondary复选框另一侧的控件Widget对象
controlAffinity①文本放置控件的位置ListTileControlAffinity枚举
controlAffinity①
取值说明
leading勾选框在开头位置
trailing勾选框在结尾位置
platform根据平台确定

3.3 示例

代码
var _checkboxListTileValue = false;
CheckboxListTile(
                subtitle: Text('Checkbox副标题'),
                secondary: Icon(Icons.person),
                controlAffinity: ListTileControlAffinity.trailing,
                title: Text('Checkbox标题'),
                value: _checkboxListTileValue,
                onChanged: (value) {
                  setState(() {
                    _checkboxListTileValue = value;
                  });
                },
              )
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值