美文网首页
flutter CheckBox 和 CheckboxListT

flutter CheckBox 和 CheckboxListT

作者: 浩仔_Boy | 来源:发表于2020-11-25 15:07 被阅读0次
    import 'package:flutter/material.dart';
    
    /*
     * CheckBox 和 CheckboxListTile
     */
    void main() {
      runApp(MyApp());
    }
    
    //自定组件就是类
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text("CheckBox 和 CheckboxListTile"),
            ),
            body: HomeContent(),
          ),
          theme: ThemeData(primarySwatch: Colors.red),
        );
      }
    }
    
    //内容区域
    class HomeContent extends StatefulWidget {
      @override
      _HomeContentState createState() => _HomeContentState();
    }
    
    class _HomeContentState extends State<HomeContent> {
      var flag = true; //是否选中
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('演示CheckBox'),
            Checkbox(
              value: this.flag,
              onChanged: (valse) {
                setState(() {
                  this.flag = valse;
                });
              },
              activeColor: Colors.black, //选中颜色
            ),
            Text(this.flag ? '选中' : '为选中'),
            Divider(),
            SizedBox(height: 20),
            Text('演示CheckBoxListTile'),
            Divider(),
            CheckboxListTile(
              value: this.flag,
              onChanged: (value) {
                setState(() {
                  this.flag = value;
                });
              },
              title: Text('标题'),
              subtitle: Text('二级标题'),
              secondary: Icon(Icons.handyman),
            ),
            Divider()
          ],
        );
      }
    }
    

    相关文章

      网友评论

          本文标题:flutter CheckBox 和 CheckboxListT

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