gpt4 book ai didi

flutter - 如何在 onPressed 按钮时从 ListView 中删除 TextField?

转载 作者:IT王子 更新时间:2023-10-29 07:19:19 26 4
gpt4 key购买 nike

当用户单击“清除图标”按钮时如何删除 TextField? (不仅仅是清除 TextField 的文本)

用户故事
用户点击一个按钮来添加玩家。 (从技术上讲,这个按钮添加了 TextField)
用户可以在TextField中写入玩家的名字。
用户单击“清除图标”按钮以删除当前的 TextField(与添加功能相反)。

enter image description here

new ListView.builder(
padding: EdgeInsets.all(0),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index) {
print(index);
return TextField(
maxLength: 20,
decoration: InputDecoration(
labelText: "Player ${index+1}",
counterText: "",
prefixIcon: const Icon(Icons.person),
suffixIcon: new IconButton(
icon: Icon(Icons.clear),
onPressed: () =>
setState(() {
this.dispose(); // -----Doesn't work----
})
),
),
);
}
),

例如,如果用户单击“清除按钮”,则用户在 Player 4 上设置“John”,然后删除 Player 4 TextField。它将只保留 4 个 TextField

enter image description here

最佳答案

我假设的事实:

  1. 您希望能够从列表中删除(或添加)一个字段
  2. 您希望在删除字段时保留剩余字段的值
  3. 列表可以大于5

解决方案:

如果您希望上述所有情况都成立,那么您实际上需要跟踪 TextFields 的 TextEditingController,而不是文本字段本身。这是因为 TextField 的值实际上存储在 TextEditingController 中(如果您不为每个小部件提供它,它将在运行时重新创建)。检查一下:

import 'package:flutter/material.dart';

// needs to be StatefulWidget, so we can keep track of the count of the fields internally
class PlayerList extends StatefulWidget {
const PlayerList({
this.initialCount = 5,
});

// also allow for a dynamic number of starting players
final int initialCount;

@override
_PlayerListState createState() => _PlayerListState();
}

class _PlayerListState extends State<PlayerList> {
int fieldCount = 0;
int nextIndex = 0;
// you must keep track of the TextEditingControllers if you want the values to persist correctly
List<TextEditingController> controllers = <TextEditingController>[];

// create the list of TextFields, based off the list of TextControllers
List<Widget> _buildList() {
int i;
// fill in keys if the list is not long enough (in case we added one)
if (controllers.length < fieldCount) {
for (i = controllers.length; i < fieldCount; i++) {
controllers.add(TextEditingController());
}
}

i = 0;
// cycle through the controllers, and recreate each, one per available controller
return controllers.map<Widget>((TextEditingController controller) {
int displayNumber = i + 1;
i++;
return TextField(
controller: controller,
maxLength: 20,
decoration: InputDecoration(
labelText: "Player $displayNumber",
counterText: "",
prefixIcon: const Icon(Icons.person),
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () {
// when removing a TextField, you must do two things:
// 1. decrement the number of controllers you should have (fieldCount)
// 2. actually remove this field's controller from the list of controllers
setState(() {
fieldCount--;
controllers.remove(controller);
});
},
),
),
);
}).toList(); // convert to a list
}


@override
Widget build(BuildContext context) {
// generate the list of TextFields
final List<Widget> children = _buildList();

// append an 'add player' button to the end of the list
children.add(
GestureDetector(
onTap: () {
// when adding a player, we only need to inc the fieldCount, because the _buildList()
// will handle the creation of the new TextEditingController
setState(() {
fieldCount++;
});
},
child: Container(
color: Colors.blue,
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(
'add player',
style: TextStyle(
color: Colors.white,
),
),
),
),
),
);

// build the ListView
return ListView(
padding: EdgeInsets.all(0),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: children,
);
}

@override
void initState() {
super.initState();

// upon creation, copy the starting count to the current count
fieldCount = widget.initialCount;
}

@override
void dispose() {
super.dispose();
}

@override
void didUpdateWidget(PlayerList oldWidget) {
super.didUpdateWidget(oldWidget);
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
}
}

通过以上,您可以:

  • 启动应用
  • 将玩家 2 更改为“bob”
  • 将玩家 3 更改为“steve”
  • 将玩家 4 更改为“查尔斯”
  • 删除玩家 3
  • 观察玩家 2 是“bob”,新玩家 3 是“charles”

我认为这就是您在这里寻找的。

关于flutter - 如何在 onPressed 按钮时从 ListView 中删除 TextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56386039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com