根据图像,我必须根据我的要求从必填字段中获取所有值,即从 TextFormField、CustomRadioButton & 以及 patient age
中获取。单击提交按钮时,我必须获取这些值,以便我可以通过提供程序传递它们。
由于我对 flutter 非常陌生,我可以从 TextFormField 中获取值,但无法从其余的两个中获取。
下面是我的用户界面。
下面是CustomRadioButton
的代码
Container(
width: 200,
child: CustomRadioButton(
customShape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
elevation: 0,
absoluteZeroSpacing: false,
unSelectedColor: Theme.of(context).canvasColor,
buttonLables: [
'Male',
'Female',
],
buttonValues: [
"MALE",
"FEMALE",
],
buttonTextStyle: ButtonTextStyle(
textStyle: TextStyle(fontSize: 10)),
radioButtonValue: (value) {
print(value);
},
width: 80,
height: 60,
enableShape: true,
selectedBorderColor: Colors.green,
unSelectedBorderColor: Color(0xFFD0D7EB),
selectedColor: Colors.green,
),
),
对于年龄选择器(/不知道我是否正确,因为我刚刚对文本 12 进行了硬编码,但希望 +
和 -
应该可以工作。/)
Container(
width: 100.0,
height: 55.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
onTap: () {},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.remove,
color: Colors.black,
size: 20.0,
),
),
),
),
Text(
'12',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 20.0,
color: Colors.black),
),
InkWell(
onTap: () {},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.add,
color: Colors.black,
size: 20.0,
),
),
),
)
],
),
)
],
),
SizedBox(height: 80),
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 40,
child: RaisedButton(
color: Color(0xFF888DA1),
child: Text('Submit', style: TextStyle(fontFamily: 'Poppins-Regular',
fontSize: 14, color: Colors.white)),
onPressed: (){
submit();
}
),
),
],
这是我将获取这些值的函数
提交()异步{ 打印(名称控制器。文本); }
请帮助我如何做到这一点!
答案 0 :(得分:1)
所以在这里我首先设置 defaultSelected: "MALE",
,然后将值存储在一个变量中,当您更改该值时
从 radioButtonValue:
这个方法获取这个值,然后简单地将此值添加到我的局部变量中。在那之后,我只添加了简单的
increment and decrement
逻辑并将此值设置为 text
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
accentColor: Colors.blue,
),
home: SelectionScreen(),
);
}
}
class SelectionScreen extends StatefulWidget{
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _selectionScreen();
}
}
class _selectionScreen extends State<SelectionScreen>{
var gender = "MALE";
int age = 12;
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
children: [
Container(
width: 200,
child: CustomRadioButton(
customShape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
elevation: 0,
absoluteZeroSpacing: false,
unSelectedColor: Theme.of(context).canvasColor,
buttonLables: [
'Male',
'Female',
],
buttonValues: [
"MALE",
"FEMALE",
],
buttonTextStyle: ButtonTextStyle(
textStyle: TextStyle(fontSize: 10)),
radioButtonValue: (value) {
setState(() {
gender = value.toString();
print("=--->>${gender}");
});
},
width: 80,
height: 60,
enableShape: true,
selectedBorderColor: Colors.green,
unSelectedBorderColor: Color(0xFFD0D7EB),
selectedColor: Colors.green,
defaultSelected: "MALE",
),
),
Container(
width: 100.0,
height: 55.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
onTap: () {
setState(() {
age--;
});
},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.remove,
color: Colors.black,
size: 20.0,
),
),
),
),
Text(
age.toString(),
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 20.0,
color: Colors.black),
),
InkWell(
onTap: () {
setState(() {
age++;
});
},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.add,
color: Colors.black,
size: 20.0,
),
),
),
)
],
),
),
SizedBox(height: 80),
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 40,
child: RaisedButton(
color: Color(0xFF888DA1),
child: Text('Submit', style: TextStyle(fontFamily: 'Poppins-Regular',
fontSize: 14, color: Colors.white)),
onPressed: (){
print("============>> Radio Button Value: $gender");
print("============>> Age: $age");
}
),
),
],),
],
),),
);
}
}
答案 1 :(得分:0)
最直接的方法是构建一个模型类,该类将具有每个表单元素的默认值。然后,在构建表单时,构建表单数据类的实例,并使用成员为每个项目建立value:
。连接每个 onChanged:
回调以验证新值,并将其存储回表单数据实例。提交时,以愉快的方式发送数据。