【Flutter】表单 - Radio, Checkbox

2019-03-25  本文已影响0人  diva_dance

Radio

image

属性

class RadioWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _RadioWidget();
  }
}
class _RadioWidget extends State<RadioWidget> {
  String _value = '';
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Radio(
              value: 'a',
              activeColor: Colors.blue,
              groupValue: _value,
              onChanged: (newValue) {
                setState(() {
                  _value = newValue;
                });
              }
            ),
            Text('开')
          ],
        ),
        Row(
          children: <Widget>[
            Radio(
                value: 'b',
                activeColor: Colors.blue,
                groupValue: _value,
                onChanged: (newValue) {
                  setState(() {
                    _value = newValue;
                  });
                }
            ),
            Text('关')
          ],
        )
      ],
    );
  }
}

Checkbox

属性

class CheckboxWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _CheckboxWidget();
  }
}
class _CheckboxWidget extends State<CheckboxWidget> {
  bool _value = false;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Checkbox(
              value: _value,
              onChanged: (newValue) {
                print('$newValue');
                setState(() {
                  _value = newValue;
                });
              },
              tristate: false,
              activeColor: Colors.red,
              checkColor: Colors.blue
            ),
          ],
        ),
      ],
    );
  }
}
上一篇下一篇

猜你喜欢

热点阅读