我对 Dart 和 Flutter 有疑问。所以我试图重构一些代码,但我陷入了引用问题。
class _LoginPageState extends State<LoginPage> {
String _email;
String _password;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text("Login Page"),
),
body: new Container(
padding: new EdgeInsets.all(16),
child: new Form(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: buildInputs()
)),
));
}
Widget buildInput(String label, String val) {
return new TextFormField(
decoration: new InputDecoration(
labelText: label,
),
validator: (value) => value.isEmpty ? "$label can't be empty" : null,
onSaved: (value) => val = value, …Run Code Online (Sandbox Code Playgroud) 默认情况下,Flutter 将所有单选按钮显示为空(未选中)。
如何设置默认选中的单选按钮?
我发布这个问题是为了记录我的解决方案,这可能会对某些人有所帮助,并且还开始讨论这个问题,因为我在这里没有找到任何相关信息。
在单选按钮代码下方:
class _ProductTypeScreen extends State<ProductType> {
String _radioValue; //Initial definition of radio button value
String choice;
void radioButtonChanges(String value) {
setState(() {
_radioValue = value;
switch (value) {
case 'one':
choice = value;
break;
case 'two':
choice = value;
break;
case 'three':
choice = value;
break;
default:
choice = null;
}
debugPrint(choice); //Debug the choice in console
});
}
// Now in the BuildContext... body widget:
@override
Widget build(BuildContext context) {
//First of the three radio …Run Code Online (Sandbox Code Playgroud) 我有附加的 AppBar,我想在标题旁边添加图像,而不是操作图标,仅添加图像,我该如何处理这个问题?
appBar: AppBar(
title: Text('Title'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.refresh),
onPressed: () {
setState(
() {
widget.athkarCategory.reset();
},
);
},
),
],
),
Run Code Online (Sandbox Code Playgroud)