代码地址
class SwitchComponent extends StatefulWidget {
SwitchComponent({Key key}) : super(key: key);
@override
_SwitchComponentState createState() => _SwitchComponentState();
}
class _SwitchComponentState extends State {
bool _switchSelected = false;
@override
Widget build(BuildContext context) {
return Container(
child: Switch(
value: _switchSelected,
onChanged: (value) {
print(value);
setState(() {
_switchSelected = value;
});
},
activeColor: Colors.red,
hoverColor: Colors.greenAccent,
),
width: 120,
);
}
}
单选框分为Radio和RadioListTile,RadioListTile主要和ListView中的ListTile一样,是一个详细的列表块。
使用方法:
// Radio的定义
const Radio({
Key key,
@required this.value,
@required this.groupValue,
@required this.onChanged,
this.activeColor,
this.focusColor,
this.hoverColor,
this.materialTapTargetSize,
this.focusNode,
this.autofocus = false,
})
// 外部定义了
String _radioData = "";
// 组件中使用
Row(
children: [
Text('男'),
Radio(
value: '男',
groupValue: this._radioData,
onChanged: (value) {
setState(() {
this._radioData = value;
});
}),
Text('女'),
Radio(
value: '女',
groupValue: this._radioData,
onChanged: (value) {
setState(() {
this._radioData = value;
});
})
],
),
效果
class RadioListTile extends StatelessWidget {
const RadioListTile({
Key key,
@required this.value,
@required this.groupValue,
@required this.onChanged,
this.activeColor,
this.title,
this.subtitle,
this.isThreeLine = false,
this.dense,
this.secondary,
this.selected = false,
this.controlAffinity = ListTileControlAffinity.platform,
})
// 定义的_radioData为String
RadioListTile(
value: '男',
groupValue: this._radioData,
subtitle: Text('性别选择'),
title: Text('选择您的性别(单选)'),
onChanged: (value) {
setState(() {
this._radioData = value;
});
}),
RadioListTile(
value: '女',
groupValue: this._radioData,
subtitle: Text('性别选择'),
title: Text('选择您的性别(单选)'),
onChanged: (value) {
setState(() {
this._radioData = value;
});
}),
效果
多选框和单选框一样,有Checkbox和CheckboxListTile两种
使用方法:
class Checkbox extends StatefulWidget {
const Checkbox({
Key key,
@required this.value,
this.tristate = false,
@required this.onChanged,
this.activeColor,
this.checkColor,
this.focusColor,
this.hoverColor,
this.materialTapTargetSize,
this.focusNode,
this.autofocus = false,
});
List
api和之前的RadioListTile的基本相似
hobbies.forEach((item) {
checkBoxListileSelection.add(CheckboxListTile(
value: item['selected'],
onChanged: (value) {
setState(() {
item['selected'] = value;
});
},
title: Text('${item['name']}'),
));
});
效果
处理输入文本
const TextField({
Key key,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
TextInputType keyboardType,
this.textInputAction,
this.textCapitalization = TextCapitalization.none,
this.style,
this.strutStyle,
this.textAlign = TextAlign.start,
this.textAlignVertical,
this.textDirection,
this.readOnly = false,
ToolbarOptions toolbarOptions,
this.showCursor,
this.autofocus = false,
this.obscureText = false,
this.autocorrect = true,
this.enableSuggestions = true,
this.maxLines = 1,
this.minLines,
this.expands = false,
this.maxLength,
this.maxLengthEnforced = true,
this.onChanged,
this.onEditingComplete,
this.onSubmitted,
this.inputFormatters,
this.enabonEditingCompleteled,
this.cursorWidth = 2.0,
this.cursorRadius,
this.cursorColor,
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.dragStartBehavior = DragStartBehavior.start,
this.enableInteractiveSelection = true,
this.onTap,
this.buildCounter,
this.scrollController,
this.scrollPhysics,
})
介绍几个常用的
其他属性还没研究过,研究到了在说
// 定义
TextEditingController _userName = TextEditingController(text: "");
TextEditingController _phoneNumber = TextEditingController();
// TextField
class _MyHomePageState extends State {
TextEditingController _userName = TextEditingController(text: "");
TextEditingController _phoneNumber = TextEditingController();
FocusNode _focusNode;
@override
void initState() {
super.initState();
// _focusNode
_focusNode = FocusNode();
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Container(
child: Column(
children: [
TextField(
controller: _userName,
decoration: InputDecoration(
hintText: '请输入您的姓名',
labelText: '用户名',
prefixIcon: Icon(Icons.person)),
autofocus: true,
style: TextStyle(
color: Colors.red[200],
),
focusNode: _focusNode,
),
TextField(
controller: _phoneNumber,
decoration: InputDecoration(
hintText: '请输入电话',
labelText: '联系电话',
icon: Icon(Icons.phone)),
inputFormatters: [
WhitelistingTextInputFormatter(RegExp("[0-9]")),
],
)
],
),
padding: EdgeInsets.all(10),
),
);
}
}
效果