首先,我很抱歉这个标题,它有点难以理解。
1 - 我动态创建表单的 TEXTFORMFIELD。
2 - 当我尝试删除 TEXTFORMFIELD(最后一个除外)时,它被删除并且 PROVIDER 中的数据发生更改,但下一个 TEXTFORMFIELD 采用已删除的 TEXTFORMFIELD 的 INITIALVALUE。
小部件代码:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/players.dart';
class PlayersNameForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
final players = Provider.of<Players>(context);
final playersData = players.players;
final playersCount = players.count();
return Form(
child: Column(
children: List.generate(playersCount, (index) =>
Container(
child: TextFormField(
initialValue: playersData[index].name,
decoration: InputDecoration(
labelText: 'Joueur ' + (index + 1).toString(),
suffixIcon: IconButton(
icon: Icon(
Icons.delete_outline,
),
onPressed: () {
players.removePlayerByIndex(index);
},
),
),
),
)
),
),
);
}
}
提供商代码:
import 'package:flutter/material.dart';
import './player.dart';
class Players with ChangeNotifier{
List<Player> _players = [
Player(
name: 'Player A'
),
Player(
name: 'Player B'
),
Player(
name: 'Player C'
),
Player(
name: 'Player D'
),
];
List<Player> get players {
return [..._players];
}
int count(){
return _players.length;
}
void addPlayer(){
int count = this.count();
if(count < 4){
_players.add(Player(name: ''));
notifyListeners();
}
}
void removePlayer(){
int count = this.count();
if(count > 2) {
_players.removeLast();
notifyListeners();
}
}
void removePlayerByIndex(index){
_players.removeAt(index);
notifyListeners();
}
}
if(controller().selectedProduct[index].category!= "other")Container(
height: h * 0.1,
width: w *
0.8, // You can adjust the width as needed
child: TextField(
// controller: controller()
// .quantityControllers[index],
textAlignVertical:
TextAlignVertical.center,
// controller: TextEditingController(
// text: controller().selectedProduct[index].qty.toString(),
// ),
// autovalidateMode:
// AutovalidateMode.always,initialValue: "",
decoration: InputDecoration(
label: Text("Category"),labelStyle: poppinsTextStyle(fontSize: t*0.6),
border: UnderlineInputBorder(),
),
// cursorHeight: h * 0.03,
style: TextStyle(fontSize: t * 0.6),
// keyboardType: TextInputType
// .number, // To bring up the number keyboard
textAlign: TextAlign.start,
// onChanged: (value) {
// print("askcd");
// controller().updateQuantityFromField(index,); // Implement this method in the controller to update the quantity
// },
onChanged: (value) {
controller().updateCategoryForProduct(index, value);
},
// validator: (value) {
// if (value == "") {
// // controller().updateQuantityFromField(index);
// return "Required";
// }
// },
),
),
SizedBox(height: h*0.000001,),
if(controller().selectedProduct[index].discount!= null)Container(
height: h * 0.1,
width: w *
0.8, // You can adjust the width as needed
child: TextField(
// controller: controller()
// .quantityControllers[index],
textAlignVertical:
TextAlignVertical.center,
// initialValue: "",
// controller: TextEditingController(
// text: controller().selectedProduct[index].qty.toString(),
// ),
// autovalidateMode:
// AutovalidateMode.always,
decoration: InputDecoration(
label: Text("Discount"),labelStyle: poppinsTextStyle(fontSize: t*0.6),
border: UnderlineInputBorder(),
),
// cursorHeight: h * 0.03,
style: TextStyle(fontSize: t * 0.6),
// keyboardType: TextInputType
// .number, // To bring up the number keyboard
textAlign: TextAlign.start,
// onChanged: (value) {
// print("askcd");
// controller().updateQuantityFromField(index,); // Implement this method in the controller to update the quantity
// },
onChanged: (value) {
controller().updateDiscontForProduct(index, value);
},
// validator: (value) {
// if (value == "") {
//// controller().updateQuantityFromField(index);
// return "Required";
// }
// },
),
),
这是代码,当我向折扣添加值然后切换类别文本字段,然后折扣值传递到类别时,我遇到了与您相同的错误(几乎与您相同的问题)。在我看来,flutter 会根据其位置考虑没有控制器的文本字段,因此我添加了最小尺寸接近 0 的大小框,瞧,我的问题解决了
视频:https://jmp.sh/s/3ISVTDHDwgq4BMnVHnXg
希望这有帮助!!