TextField
[
](https://book.flutterchina.club/chapter3/input_and_form.html)
TextField的样式 decoration
TextField(onChanged: (value) {//输入内容改变时的回调,value是变化后文本框里的内容debugPrint('input $value');},onSubmitted: (value) {//点击确定时的回调,value是文本框里所有的内容debugPrint('submit $value');},decoration: InputDecoration(icon: Icon(Icons.subject),labelText: 'Title', //输入框上方的titlehintText: 'Enter Post Title', //placeText// border: InputBorder.none,//设置文本框的下边框无border: OutlineInputBorder(), //四周外边框fillColor: Colors.grey[400], //填充的背景色filled: true, //是否填充背景色),);
监听文本变化
- 使用TextEditingController监听文本字段变化 ```dart
class TextFieldDemo extends StatefulWidget { @override _TextFieldDemoState createState() => _TextFieldDemoState(); }
class _TextFieldDemoState extends State
@override void dispose() { // TODO: implement dispose textEditingController.dispose(); //释放textEditingController super.dispose(); }
@override void initState() { // TODO: implement initState super.initState(); textEditingController.text = ‘Hi’; textEditingController.addListener( () { debugPrint(‘input ${textEditingController.text}’); }, ); }
@override Widget build(BuildContext context) { return TextField( controller: textEditingController,
onSubmitted: (value) {//点击确定时的回调,value是文本框里所有的内容debugPrint('submit $value');},decoration: InputDecoration(icon: Icon(Icons.subject),labelText: 'Title', //输入框上方的titlehintText: 'Enter Post Title', //placeText// border: InputBorder.none,//设置文本框的下边框无border: OutlineInputBorder(), //四周外边框fillColor: Colors.grey[400], //填充的背景色filled: true, //是否填充背景色),);
} }
- 设置onChange回调```dartTextField(autofocus: true,onChanged: (v) {print("onChange: $v");})
控制焦点
- 通过FocusNode和FocusScopeNode来控制 ```dart
class FocusTestRoute extends StatefulWidget { @override _FocusTestRouteState createState() => new _FocusTestRouteState(); }
class _FocusTestRouteState extends State
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children:
focusNode1.unfocus();
focusNode2.unfocus();
},
),
],
);
},
),
],
),
);
}
}
- 监听焦点状态改变事件```dart// 创建 focusNodeFocusNode focusNode = new FocusNode();...// focusNode绑定输入框TextField(focusNode: focusNode);...// 监听焦点变化focusNode.addListener((){//获得焦点时focusNode.hasFocus值为true,失去焦点时为false。print(focusNode.hasFocus);});
Form 表单
class RegisterForm extends StatefulWidget {@override_RegisterFormState createState() => _RegisterFormState();}class _RegisterFormState extends State<RegisterForm> {final registerFormKey = GlobalKey<FormState>(); //创建FormKeyString username, password;bool autovalidate = false;void submitRegisterForm() {if (registerFormKey.currentState.validate()) {registerFormKey.currentState.save(); //调用onSaveregisterFormKey.currentState.validate(); //调用验证debugPrint('username : $username , password : $password');} else {setState(() {autovalidate = true;});}}String validatorUsername(value) {if (value.isEmpty) {return 'Username is required';}return null;}String validatorPassword(value) {if (value.isEmpty) {return 'Password is required';}return null;}@overrideWidget build(BuildContext context) {return Form(key: registerFormKey, //指定可以child: Column(children: <Widget>[TextFormField(decoration: InputDecoration(labelText: 'Username',helperText: '',),onSaved: (value) {username = value; //获取TextField的值},validator: validatorUsername, //添加验证函数autovalidate: autovalidate,),TextFormField(obscureText: true, //密码的圆点显示decoration: InputDecoration(labelText: 'Password',helperText: '',),onSaved: (value) {password = value; //获取TextField的值},validator: validatorPassword, //添加验证函数autovalidate: autovalidate,),SizedBox(height: 32.0,),Container(width: double.infinity,child: RaisedButton(color: Theme.of(context).accentColor,child: Text('Register', style: TextStyle(color: Colors.white)),elevation: 0.0,onPressed: submitRegisterForm,),)],),);}}
自定义案例
import 'package:flutter/material.dart';///主色调const MaterialColor primary = const MaterialColor(0xfffb7299,const <int, Color>{50: const Color(0xffff9db5)},);///登录输入框,自定义widgetclass LoginInput extends StatefulWidget {final String title;final String hint;final ValueChanged<String> onChanged;final ValueChanged<bool> focusChanged;final bool lineStretch;final bool obscureText;final TextInputType keyboardType;const LoginInput(this.title, this.hint,{Key key,this.onChanged,this.focusChanged,this.lineStretch = false,this.obscureText = false,this.keyboardType}): super(key: key);@override_LoginInputState createState() => _LoginInputState();}class _LoginInputState extends State<LoginInput> {final _focusNode = FocusNode();@overridevoid initState() {super.initState();//是否获取光标的监听_focusNode.addListener(() {print("Has focus: ${_focusNode.hasFocus}");if (widget.focusChanged != null) {widget.focusChanged(_focusNode.hasFocus);}});}@overridevoid dispose() {_focusNode.dispose();super.dispose();}@overrideWidget build(BuildContext context) {return Column(children: [Row(children: [Container(padding: EdgeInsets.only(left: 15),width: 100,child: Text(widget.title,style: TextStyle(fontSize: 16),),),_input()],),Padding(padding: EdgeInsets.only(left: !widget.lineStretch ? 15 : 0),child: Divider(height: 1,thickness: 0.5,),)],);}_input() {return Expanded(child: TextField(focusNode: _focusNode,onChanged: widget.onChanged,obscureText: widget.obscureText,keyboardType: widget.keyboardType,autofocus: !widget.obscureText,cursorColor: primary,style: TextStyle(fontSize: 16, color: Colors.black, fontWeight: FontWeight.w300),//输入框的样式decoration: InputDecoration(contentPadding: EdgeInsets.only(left: 20, right: 20),border: InputBorder.none,hintText: widget.hint ?? '',hintStyle: TextStyle(fontSize: 15, color: Colors.grey)),));}}
复选框
Checkbox
var checkbox = Checkbox(value: _checkBoxA,onChanged: (value) {setState(() {_checkBoxA = value;});},activeColor: Colors.black,//勾选状态的颜色,默认是主题下的Theme.of(context).accentColor);
CheckboxListTile
var checkboxListTile = CheckboxListTile(value: _checkBoxA,onChanged: (value) {setState(() {_checkBoxA = value;});},title: Text('Checkbox Item A'),//标题文字subtitle: Text('Description'),//副标题secondary: Icon(Icons.bookmark),//显示的图标selected: _checkBoxA,//是否处于激活状态);
单选框
Radio
class RadioDemo extends StatefulWidget {@override_RadioDemoState createState() => _RadioDemoState();}class _RadioDemoState extends State<RadioDemo> {int _radioGroupA = 0;void _handleRadioValueChanged(int value) {setState(() {_radioGroupA = value;});}@overrideWidget build(BuildContext context) {var radioDemo = Row(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Radio(value: 0,groupValue: _radioGroupA,//权组值onChanged: _handleRadioValueChanged,activeColor: Colors.black,//选中的颜色),Radio(value: 1,groupValue: _radioGroupA,//权组值onChanged: _handleRadioValueChanged,activeColor: Colors.black,//选中的颜色)],);return Scaffold(appBar: AppBar(title: Text('RadioDemo'),elevation: 0.0,),body: Container(padding: EdgeInsets.all(16.0),child: Column(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[radioDemo,],),),);}}
RadioListTile
Column(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Text('RadioGroupValue:$_radioGroupA'),SizedBox(height: 32.0),RadioListTile(value: 0,groupValue: _radioGroupA,onChanged: _handleRadioValueChanged,title:Text('Option A') ,subtitle: Text('Description'),secondary: Icon(Icons.filter_1),//右侧图标selected: _radioGroupA == 0,),RadioListTile(value: 1,groupValue: _radioGroupA,onChanged: _handleRadioValueChanged,title:Text('Option B') ,subtitle: Text('Description'),secondary: Icon(Icons.filter_2),//右侧图标selected: _radioGroupA == 1,),],),
Switch和Slider
Switch
import 'package:flutter/material.dart';class SwitchDemo extends StatefulWidget {@override_SwitchDemoState createState() => _SwitchDemoState();}class _SwitchDemoState extends State<SwitchDemo> {bool _switchA = false;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('SwitchDemo'),elevation: 0.0,),body: Container(padding: EdgeInsets.all(16.0),child: Column(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Text(_switchA ? '☺' :'😭',style: TextStyle(fontSize: 32.0)),Switch(value: _switchA,onChanged: (value) {setState(() {_switchA = value;});},)],),),);}}
SwitchListTile
SwitchListTile(value: _switchA,onChanged: (value) {setState(() {_switchA = value;});},title: Text('Switch Item A'),subtitle:Text('Description'),secondary: _switchA ? Icon(Icons.visibility):Icon(Icons.visibility_off),selected: _switchA,)
Slider
Slider(value: _sliderA,onChanged: (value) {setState(() {_sliderA = value;});},min: 0.0,max: 10.0,divisions: 10,activeColor: Theme.of(context).accentColor,inactiveColor: Theme.of(context).accentColor.withOpacity(0.3),label: '${_sliderA.toInt()}',)
