美文网首页
Flutter-CheckBox与CheckBoxListTil

Flutter-CheckBox与CheckBoxListTil

作者: 梦幽辰 | 来源:发表于2020-01-01 19:09 被阅读0次

    单独一个复选框(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,则图标和文字均改变颜色
                ),
              ],
            ),
          ),
        );
      }
    }
    
    
    预览

    相关文章

      网友评论

          本文标题:Flutter-CheckBox与CheckBoxListTil

          本文链接:https://www.haomeiwen.com/subject/rkrgoctx.html