我正在制作体育馆日记,每步动作都有“卡片”,其中包括“ setsreps kg”。因此,代码在视图中生成了尽可能多的卡“ Squat”和“ Front-squat”。这里的问题是我在两张卡中都使用controllerSets。如果我将“下蹲”设置编号更改为“ 2”,则“前蹲”设置编号也更改为“ 2”,因为它使用相同的controllerSets TextEditingController。这是我的问题,请考虑甚至可以有10个移动,我不希望为每个移动创建10 x 3控制器。我的问题是我应该如何构建此功能?完整的代码可以在这里找到:
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
],
),
],
);
Run Code Online (Sandbox Code Playgroud)
}
让arrays你TextEditingControllers像这样:
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');
Run Code Online (Sandbox Code Playgroud)
在你的initState方法中,执行以下操作:
@override
void initState() {
super.initState();
}
Run Code Online (Sandbox Code Playgroud)
现在将其传递到函数list index中_saveMove,因此它看起来像这样:
_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);
});
}
Run Code Online (Sandbox Code Playgroud)
您的_buildCardContent()函数将被更改,因为您还必须index传递:controllers list
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
],
),
],
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
224 次 |
| 最近记录: |