25.Flutter中的表单 Radio RadioListTile Switch SwitchListTile 以及表单组件实现一个简单的学员登记系统(下)

四、Radio、RadioListTile单选按钮组件
Radio常用属性:
value单选的值。
onChanged改变时触发。
activeColor:选中的颜色、背景颜色
groupValue:选择组的值。

RadioListTile:常用属性:
value:true或者false
onChanged:改变的时候触发的事件。
activeColor:选中的颜色、背景颜色
title:标题
subtitle:二级标题
secondary:配置图标或者图片
groupValue:选择租的值。
五、开关Switch
 
Radio:
import 'package:flutter/material.dart';
class RadioDemoPage extends StatefulWidget {
  RadioDemoPage({Key key}) : super(key: key);

  _RadioDemoPageState createState() => _RadioDemoPageState();
}

class _RadioDemoPageState extends State<RadioDemoPage> {
  int sex=1;
  bool flag=true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        title: Text('Radio'),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child:Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                //groupValue:一样表示是同一个按钮组:
                Text(''),
                Radio(
                  value: 1,
                  onChanged:(v){
                    setState(() {
                      this.sex=v;
                    });
                  },
                  groupValue: this.sex,
                ),
                SizedBox(20),
                Text(''),
                Radio(
                  value: 2,
                  onChanged:(v){
                    setState(() {
                      this.sex=v;
                    });
                  },
                  groupValue: this.sex,
                )
              ],
            ),
            Row(
              children: <Widget>[
                Text("${this.sex}"),
                Text(this.sex==1?'':'')
              ],
            ),
            SizedBox(height: 40),
            RadioListTile(
              value: 1,
              onChanged: (v){
                setState(() {
                  this.sex=v;
                });
              },
              groupValue: this.sex,
              title: Text('标题'),
              subtitle: Text('二级标题'),
              secondary: Icon(Icons.help),
            ),
            RadioListTile(
              value: 2,
              onChanged: (v){
                setState(() {
                  this.sex=v;
                });
              },
              groupValue: this.sex,
              title: Text('标题'),
              subtitle: Text('二级标题'),
              secondary: Icon(Icons.help),
            ),
            SizedBox(height: 40),
            Switch(
              value:this.flag,
              onChanged: (v){
                setState(() {
                  print(v);
                  this.flag=v;
                });
              },
            )
          ],
        ),
      ),
    );
  }
}

FormDemo.dart

import 'package:flutter/material.dart';

class FormDemoPage extends StatefulWidget {
  FormDemoPage({Key key}) : super(key: key);

  _FormDemoPageState createState() => _FormDemoPageState();
}

class _FormDemoPageState extends State<FormDemoPage> {
  String username;
  int sex = 1;
  String info;
  List hobby = [
    {"checked": true, "title": "吃饭"},
    {"checked": true, "title": "睡觉"},
    {"checked": true, "title": "写代码"}
  ];
  List<Widget> _getHobby() {
    List<Widget> tempList = [];
    for (var i = 0; i < this.hobby.length; i++) {
      tempList.add(Row(
        children: <Widget>[
          Text(this.hobby[i]['title'] + ':'),
          Checkbox(
            value: this.hobby[i]['checked'],
            onChanged: (value) {
              setState(() {
                this.hobby[i]['checked'] = value;
              });
            },
          )
        ],
      ));
    }
    return tempList;
  }

  void _sexChanged(value) {
    setState(() {
      this.sex = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text('学员信息登记系统'),
        ),
        body: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            children: <Widget>[
              TextField(
                decoration: InputDecoration(hintText: "输入用户信息"),
                onChanged: (value) {
                  setState(() {
                    this.username = value;
                  });
                },
              ),
              Row(
                children: <Widget>[
                  Text(""),
                  Radio(
                    value: 1,
                    onChanged: this._sexChanged,
                    groupValue: this.sex,
                  ),
                  Text(""),
                  Radio(
                    value: 2,
                    onChanged: this._sexChanged,
                    groupValue: this.sex,
                  )
                ],
              ),
              //爱好:
              Column(
                children: this._getHobby(),
              ),
              TextField(
                maxLines: 4,
                decoration: InputDecoration(
                  hintText: "描述信息",
                  border: OutlineInputBorder()
                ),
                onChanged: (value){
                  setState(() {
                    this.info=value;
                  });
                },
              ),
              SizedBox(height: 20),
              Container(
                 double.infinity,
                height: 40,
                child: RaisedButton(
                  child: Text("登录"),
                  onPressed: () {
                    print(this.sex);
                    print(this.username);
                    print(this.hobby);
                    print(this.info);
                  },
                  color: Colors.blue,
                  textColor: Colors.white,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
原文地址:https://www.cnblogs.com/yiweiyihang/p/11534890.html