首页 > 建站教程 > APP开发,混合APP >  Flutter笔记18:CheckBox复选框正文

Flutter笔记18:CheckBox复选框

Flutter的CheckBox复选框和html的差不多,有选中和未选中,只不过这里使用了StateFulWidget方便变化视图:
class DemoPage extends StatefulWidget {
  DemoPage({Key key}) : super(key: key);

  @override
  _DemoPageState createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage> {
  var currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Checkbox(
            //激活时的颜色
            activeColor: Colors.red,
            //是否被选中
            value: 0 == currentIndex,
            //复选框是否有空值,如果为true,则有true、false和空三个值,false则只有true和false两个值
            tristate: false,
            onChanged: (bool check) {
              setState(() {
                if (check) {
                  currentIndex = 0;
                }
              });
            }),
        Checkbox(
            //激活时的颜色
            activeColor: Colors.red,
            //是否被选中
            value: 1 == currentIndex,
            onChanged: (bool check) {
              setState(() {
                if (check) {
                  currentIndex = 1;
                }
              });
            }),
      ],
    );
  }
}