英文:
how to create dynamic number of text editing controllers with unique names
问题
class PlayersIntroductionScreen extends StatelessWidget {
const PlayersIntroductionScreen({Key? key, required this.playersNumber}) : super(key: key);
final int playersNumber;
@override
Widget build(BuildContext context) {
List<TextEditingController> controllers = List.generate(
playersNumber, (index) => TextEditingController());
// Use controllers[index] for TextFormField() widgets
return Placeholder();
}
}
英文:
I am trying to make game score tracked game, and I have a page where the user enters the number of players and go to another page to enter their names, so he is going to PlayersIntroductionScreen() with the number of players, in the players introduction screen I need to display several TextFormField() based on the players number and I can made this with for(), but the problem is I need a TextEditingController() for each TextFormField()
class PlayersIntroductionScreen extends StatelessWidget {
const PlayersIntroductionScreen({Key? key, required this.playersNumber}) : super(key: key);
final int playersNumber;
@override
Widget build(BuildContext context) {
return Placeholder();
}
}
I tried to make a for loop before the overriden build() but it says Expected a class member.
How can I achieve that with unique name for every controller like
var firstPlayerController = TextEditingController()
and so on
答案1
得分: 0
要在Flutter中创建具有唯一名称的动态文本编辑控制器的数量,您可以使用一个`List`来存储这些控制器,并根据它们在列表中的位置为每个`controller`分配一个唯一的名称。
class PlayersIntroductionScreen extends StatefulWidget {
const PlayersIntroductionScreen({Key? key, required this.playersNumber}) : super(key: key);
final int playersNumber;
@override
State<PlayersIntroductionScreen> createState() => _PlayersIntroductionScreenState();
}
class _PlayersIntroductionScreenState extends State<PlayersIntroductionScreen> {
List<TextEditingController> controllers = [];
@override
void initState() {
super.initState();
createControllers(widget.playersNumber);
}
@override
void dispose() {
// TODO: 实现dispose
disposeControllers();
super.dispose();
}
void createControllers(int numberOfControllers) {
for (int i = 0; i < numberOfControllers; i++) {
TextEditingController controller = TextEditingController();
controllers.add(controller);
}
}
void disposeControllers() {
for (TextEditingController controller in controllers) {
controller.dispose();
}
}
@override
Widget build(BuildContext context) {
return buildTextFields();
}
Widget buildTextFields() {
return Column(
children: List.generate(controllers.length, (index) {
return TextField(
controller: controllers[index],
decoration: InputDecoration(
hintText: '文本框 ${index + 1}',
),
);
}),
);
}
}
英文:
To create a dynamic number of text editing controllers with unique names in Flutter, you can use a List
to store the controllers and assign a unique name to each controller
based on its position in the list.
class PlayersIntroductionScreen extends StatefulWidget {
const PlayersIntroductionScreen({Key? key, required this.playersNumber}) : super(key: key);
final int playersNumber;
@override
State<PlayersIntroductionScreen> createState() => _PlayersIntroductionScreenState();
}
class _PlayersIntroductionScreenState extends State<PlayersIntroductionScreen> {
List<TextEditingController> controllers = [];
@override
void initState() {
super.initState();
createControllers(widget.playersNumber);
}
@override
void dispose() {
// TODO: implement dispose
disposeControllers();
super.dispose();
}
void createControllers(int numberOfControllers) {
for (int i = 0; i < numberOfControllers; i++) {
TextEditingController controller = TextEditingController();
controllers.add(controller);
}
}
void disposeControllers() {
for (TextEditingController controller in controllers) {
controller.dispose();
}
}
@override
Widget build(BuildContext context) {
return buildTextFields();
}
Widget buildTextFields() {
return Column(
children: List.generate(controllers.length, (index) {
return TextField(
controller: controllers[index],
decoration: InputDecoration(
hintText: 'Text Field ${index + 1}',
),
);
}),
);
}
}
答案2
得分: 0
每个文本字段将由一个具有唯一实例的独特控制器进行管理。所以我认为你不需要为每个字段添加名称。(你的玩家没有输入他们的名字,所以他们仍然是匿名的)
你可以使用一个List<TextEditingController>来管理每个字段,并通过索引访问。
你的类可以是:
class PlayersIntroductionScreen extends StatefulWidget {
final int numberOfPlayer;
const PlayersIntroductionScreen({Key? key, required this.numberOfPlayer});
@override
State<StatefulWidget> createState() => _PlayersIntroductionScreenState();
}
class _PlayersIntroductionScreenState extends State<PlayersIntroductionScreen> {
final List<TextEditingController> _controllers = [];
@override
void initState() {
super.initState();
for (var i = 0; i < widget.numberOfPlayer; i++) {
final controller = TextEditingController()
..addListener(() {
setState(() {});
});
_controllers.add(controller);
}
}
@override
void dispose() {
_controllers.forEach((e) {e.dispose();});
super.dispose();
}
}
英文:
Each text field will be manage by a unique controller with unique instance. So i think you no need to add name for each of these. (Your player isn't place their name, so all of them still anonymous)
You could make a List<TextEdittingController> to manage each field and access by index.
Your class could be
class PlayersIntroductionScreen extends StatefulWidget {
final int numberOfPlayer;
const PlayersIntroductionScreen({super.key, required this.numberOfPlayer});
@override
State<StatefulWidget> createState() => _PlayersIntroductionScreenState();
}
class _PlayersIntroductionScreenState extends State<PlayersIntroductionScreen> {
final List<TextEditingController> _controllers = [];
@override
void initState() {
super.initState();
for (var i = 0; i < widget.numberOfPlayer; i++) {
final controller = TextEditingController()
.addListener(() {
setState(() {});
});
_controllers.add(controller);
}
}
@override
void dispose() {
_controllers.forEach((e) {e.dispose();});
super.dispose();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论