Flutter 所有卡的文本字段值更改
我正在写健身日记,每个动作都有“卡片”,其中包括“套数/公斤”。因此,代码在视图中生成尽可能多的卡“蹲”和“前蹲”。这里的问题是,我在两张卡中都使用控制器集。如果我将“下蹲”设置编号更改为“2”,那么“前蹲”编号也将更改为“2”,因为它使用相同的控制器集TextEditingController。这是我的问题,请考虑,甚至可以有10个动作,我不认为我想创建10×3控制器的每一个。我的问题是我应该如何构建这个特性?完整代码可在此处找到:Flutter 所有卡的文本字段值更改,flutter,dart,Flutter,Dart,我正在写健身日记,每个动作都有“卡片”,其中包括“套数/公斤”。因此,代码在视图中生成尽可能多的卡“蹲”和“前蹲”。这里的问题是,我在两张卡中都使用控制器集。如果我将“下蹲”设置编号更改为“2”,那么“前蹲”编号也将更改为“2”,因为它使用相同的控制器集TextEditingController。这是我的问题,请考虑,甚至可以有10个动作,我不认为我想创建10×3控制器的每一个。我的问题是我应该如何构建这个特性?完整代码可在此处找到: return Column( children: <
return Column(
children: <Widget>[
Text(
_programMovesGen(program)[index],
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
), // Move name
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
child: Padding(
padding: const EdgeInsets.all(18.0),
child: new TextFormField(
controller: controllerSets,
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(2),
]),
),
),
Text(
"sets ",
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
),
new Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
controller: controllerReps,
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(2),
]),
),
),
Text(
"reps",
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
),
new Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
controller: controllerKgs,
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(2),
]),
),
),
Text(
"kg",
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
),
Container(
height: 60,
width: 60,
margin: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.pink,
width: 3.5,
),
),
child: IconButton(
icon: Icon(
IconData(57669, fontFamily: 'MaterialIcons'),
size: 38,
color: Colors.red,
),
onPressed: () => {
_saveMove(_programMovesGen(program)[index]),
})),
///TODO add to db and previous added move
],
),
],
);
返回列(
儿童:[
正文(
_程序移动(程序)[索引],
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),//移动名称
划船(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
新柔性(
孩子:填充(
填充:常数边集全部(18.0),
子项:新建TextFormField(
控制器:控制器集,
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(2),
]),
),
),
正文(
“集”,
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),
新柔性(
孩子:填充(
填充:常数边集全部(8.0),
子项:新建TextFormField(
控制器:ControllerEPS,
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(2),
]),
),
),
正文(
“代表”,
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),
新柔性(
孩子:填充(
填充:常数边集全部(8.0),
子项:新建TextFormField(
控制器:控制器KGS,
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(2),
]),
),
),
正文(
“千克”,
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),
容器(
身高:60,
宽度:60,
边距:所有常数边集(16.0),
装饰:盒子装饰(
形状:BoxShape.circle,
边界:边界(
颜色:颜色。粉红色,
宽度:3.5,
),
),
孩子:我的钮扣(
图标:图标(
Iconda(57669,fontFamily:“唯物主义者”),
尺码:38,
颜色:颜色,红色,
),
按下:()=>{
_saveMove(_programMovesGen(program)[index]),
})),
///待办事项添加到数据库和以前添加的移动
],
),
],
);
}
将
文本编辑控制器的数组制作成如下:
List<TextEditingController> controllerSets = new List<TextEditingController>();
List<TextEditingController> controllerReps = new List<TextEditingController>();
List<TextEditingController> controllerKgs = new List<TextEditingController>();
TextEditingController sets = new TextEditingController(text: '3');
TextEditingController reps = new TextEditingController(text: '6');
TextEditingController kgs = new TextEditingController(text: '60');
_saveMove(String moveName, int index) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
debugPrint("BEFORE SAVEDMOVES " + savedMoves);
savedMoves += (moveName != "")
? moveName +
": " +
controllerSets[index].text +
" sets " +
controllerReps[index].text +
" reps " +
controllerKgs[index].text +
" kg\n"
: "";
prefs.setString('history', savedMoves);
});
}
现在,在\u saveMove
函数中传递列表索引
,它将如下所示:
List<TextEditingController> controllerSets = new List<TextEditingController>();
List<TextEditingController> controllerReps = new List<TextEditingController>();
List<TextEditingController> controllerKgs = new List<TextEditingController>();
TextEditingController sets = new TextEditingController(text: '3');
TextEditingController reps = new TextEditingController(text: '6');
TextEditingController kgs = new TextEditingController(text: '60');
_saveMove(String moveName, int index) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
debugPrint("BEFORE SAVEDMOVES " + savedMoves);
savedMoves += (moveName != "")
? moveName +
": " +
controllerSets[index].text +
" sets " +
controllerReps[index].text +
" reps " +
controllerKgs[index].text +
" kg\n"
: "";
prefs.setString('history', savedMoves);
});
}
您的\u buildCardContent()
函数将被更改,因为您还必须通过控制器列表中的索引
:
Widget _buildCardContent(int index) {
controllerSets.add(sets);
controllerReps.add(reps);
controllerKgs.add(kgs);
return Column(
children: <Widget>[
Text(
_programMovesGen(program)[index],
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
), // Move name
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
child: Padding(
padding: const EdgeInsets.all(18.0),
child: new TextFormField(
controller: controllerSets[index],
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(2),
]),
),
),
Text(
"sets ",
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
),
new Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
controller: controllerReps[index],
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(2),
]),
),
),
Text(
"reps",
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
),
new Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
controller: controllerKgs[index],
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(2),
]),
),
),
Text(
"kg",
textAlign: TextAlign.center,
style: TextStyle().copyWith(color: Colors.black, fontSize: 18.0),
),
Container(
height: 60,
width: 60,
margin: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.pink,
width: 3.5,
),
/*gradient: RadialGradient(
radius: 0.50,
colors: [
Colors.blue,
Colors.yellowAccent,
],
),*/
),
child: IconButton(
icon: Icon(
IconData(57669, fontFamily: 'MaterialIcons'),
size: 38,
color: Colors.red,
),
onPressed: () => {
_saveMove(_programMovesGen(program)[index], index),
})),
///TODO add to db and previous added move
],
),
],
);
}
Widget\u buildCardContent(int索引){
控制器集。添加(组);
controllerReps.add(reps);
控制器千克。添加(千克);
返回列(
儿童:[
正文(
_程序移动(程序)[索引],
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),//移动名称
划船(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
新柔性(
孩子:填充(
填充:常数边集全部(18.0),
子项:新建TextFormField(
控制器:控制器集[索引],
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(2),
]),
),
),
正文(
“集”,
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),
新柔性(
孩子:填充(
填充:常数边集全部(8.0),
子项:新建TextFormField(
控制器:controllereps[索引],
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(2),
]),
),
),
正文(
“代表”,
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),
新柔性(
孩子:填充(
填充:常数边集全部(8.0),
子项:新建TextFormField(
控制器:控制器KGS[索引],
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(2),
]),
),
),
正文(
“千克”,
textAlign:textAlign.center,
style:TextStyle().copyWith(颜色:Colors.black,fontSize:18.0),
),
容器(
身高:60,
//in initstate
_setsController.text = widget.program.sets;
//do this for the other two controllers
return Scaffold(
body: ListView(
children: <Widget>[
GymCard(program:programs[0],excerciseName : 'Squat'),
GymCard(program:programs[1],excerciseName : 'bench press') //and so on
]
)
)