【问题标题】:How to add different functions for the buttons generated using a for loop in flutter如何为在颤振中使用 for 循环生成的按钮添加不同的功能
【发布时间】:2022-07-20 18:47:46
【问题描述】:

在颤振中,我需要生成一些按钮。我使用 for 循环生成了按钮,但在这种情况下,我无法为使用 for 循环生成的每个按钮添加不同的功能。我需要为每个按钮赋予不同的功能。

【问题讨论】:

  • 您可以创建一个数组,其中包含与您的按钮具有相同索引的所有功能

标签: flutter flutter-web


【解决方案1】:

有一种方法可以在给定条件的情况下执行与条件相对应的功能。

 ///Create Button
  _customTextButton(int num){

    switch(num){
      case 0:
        print('Clicked Button 1');
        break;
      case 1:
        print('Clicked Button 1');
        break;
      case 2:
        print('Clicked Button 1');
        break;
      default:
        print('Unknown!');
        break;
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('test'),
      ),
      body: Column(
        children: <Widget>[

          for(int i=0; i<3; i++)
            TextButton(
              child: Text('Button'),
              onPressed:() => _customTextButton(i),
            ),

        ],
      ),
    );
  }

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2023-03-16
    相关资源
    最近更新 更多