如何创建具有唯一名称的动态文本编辑控制器数量?

huangapple go评论126阅读模式
英文:

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&lt;PlayersIntroductionScreen&gt; createState() =&gt; _PlayersIntroductionScreenState();
}

class _PlayersIntroductionScreenState extends State&lt;PlayersIntroductionScreen&gt; {

  List&lt;TextEditingController&gt; 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 &lt; 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: &#39;Text Field ${index + 1}&#39;,
          ),
        );
      }),
    );
  }
}

答案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&lt;StatefulWidget&gt; createState() =&gt; _PlayersIntroductionScreenState();
}

class _PlayersIntroductionScreenState extends State&lt;PlayersIntroductionScreen&gt; {
  final List&lt;TextEditingController&gt; _controllers = [];

  @override
  void initState() {
    super.initState();
    for (var i = 0; i &lt; widget.numberOfPlayer; i++) {
      final controller = TextEditingController()
        .addListener(() {
          setState(() {});
      });
      _controllers.add(controller);
    }
  }

  @override
  void dispose() {
    _controllers.forEach((e) {e.dispose();});
    super.dispose();
  }
}

huangapple
  • 本文由 发表于 2023年4月11日 09:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定