【问题标题】:How do I assign an index to list of buttons in the listview?如何为列表视图中的按钮列表分配索引?
【发布时间】:2021-10-12 03:29:52
【问题描述】:
Widget inEngTaskBody(BuildContext context) {

return ListView(
scrollDirection: Axis.vertical,
children: <Widget>[

  Center(
    child: Text(
      'General Condition',
      style: TextStyle(
        decoration: TextDecoration.underline,
        fontSize: 25,
      ),
    ), //Text
  ), // Center

  OutlinedButton(
    child: Text(inEngTaskList().genCond[0]),
    onPressed: () {},
  ), //GCbutton1
  SizedBox(height: 10),

  OutlinedButton(
    child: Text(inEngTaskList().genCond[1]),
    onPressed: () {},
  ), //GCbutton2
  SizedBox(height: 10),

  OutlinedButton(
    child: Text(inEngTaskList().genCond[2]),
    onPressed: () {},
  ), //GCbutton3
  SizedBox(height: 10),

如何为每个按钮分配索引?正在考虑使用scrolltoindex() 函数。我已经看到很多使用listview.builder 的示例,但是,我正在制作自定义列表视图。我不打算使用构建器,因为每个按钮在onpressed() 上都有特定的任务。许多示例都是非按钮列表视图。

感谢您的帮助。

此代码将分配给

body: inEngTask(context)

在我的main.dart.

使用 Bernard Hur 的建议:

我正在努力实现的目标。

编辑 每个按钮中的文本都存储在一个列表中。如果我可以索引每个按钮,我可以创建一个搜索栏来与列表进行比较并滚动到按钮的索引。但是,我确实需要一些文本作为某些按钮的标题,因此循环 listview.builder 是一个挑战。

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    我不确定您想要自动滚动列表视图还是仅通过索引构建的列表视图。下面是index制作的listview。

    ListView.builder(
      itemCount: inEngTaskList().length,
      itemBuilder: (context, index) {
      final String text = inEngTaskList().genCond[index] as String;
    
      return Column(
        children: [
          const Center(
          child: Text(
            'General Condition',
            style: TextStyle(
            decoration: TextDecoration.underline,
            fontSize: 25,
            ),
           ), //Text
          ), // Center
    
          OutlinedButton(
            child: Text(text),
            onPressed: () {},
          ),
          const SizedBox(height: 10),
    
          ],
        );
       },
      );
    
    

    【讨论】:

    • 你好,我试过你的方法。它给了我上面的图片。感谢您的帮助。
    • 很高兴听到这个消息^ㅁ^
    • 嗨,伯纳德,我很抱歉。我认为我犯了一个错误,因为不够清楚。我上传了一张我正在努力实现的照片。我不确定 ListView.builder 在这种情况下是否合适。有解决办法吗?我在没有 listview 的情况下完成了上述操作,但我无法索引每个按钮。感谢您的帮助。谢谢。
    • 嗨!回复晚了非常抱歉。问题解决了?如果没有,请给我发邮件。 (hurgoon@gmail.com) 我无法清楚地解决新问题。
    【解决方案2】:

    尝试使用基于索引的条件 ..!!

     ListView.builder(
                itemCount: 8,
                itemBuilder: (context, index) {
                  if (index == 0) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 1) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 2) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 3) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 4) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 5) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 6) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  }
                  if (index == 7) {
                    return Container(
                      child: Text('Text$index'),
                    );
                  } else {
                    return Container(
                      child: Text('Text'),
                    );
                  }
                },
              ),
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案3】:

    您可以使用ListView.builder 代替ListView(children:...) 版本如下:

    ListView.builder(
      padding: const EdgeInsets.all(8),
      itemCount: NumberOfButtons, // for example 3
      itemBuilder: (BuildContext context, int index) {
        return Column(
          children:[
             Text("your header"),
             // you can add other widgets as well
             OutlinedButton(
                child: Text(inEngTaskList().genCond[index]),
                onPressed: ()=>mainFunction(index),
             ), 
          ]
        );
      }
    );
    

    更多示例请参见 [this][1]。此外,关于主要功能,您有两个选择:

    1. 拥有像functionList 这样的函数列表,并根据列表视图中项目的索引onClick:functionList[index] 使用它们。
    2. 或使用mainFunction,它根据给定的index 决定操作,如上面的示例。您可以基于此函数处理不同的事件:
    void mainFunction(int index){
       if(index==0){//do sth}
       else if(index==1){//do sth else}
       // and so on...
    }
    
    
      [1]: https://api.flutter.dev/flutter/widgets/ListView-class.html
    

    【讨论】:

    • 您好,但这不允许我为每个按钮提供自定义 onPressed 指令,因为这就像一个循环,生成按钮。如果我错了,请指教。另外,我将如何包含文本标题?感谢您的帮助。
    • 哦,其实我只是举个例子。您可以根据需要添加更多功能。我已经更新了我的答案,因此您可以了解如何处理每个按钮的不同方法,以及如何使用Column(或Row)为ListView 的每个元素提供各种小部件。如果您有任何其他问题,请告诉我。
    • 您好 Abbasihsn,感谢您的回复!我了解了 onpressed 的功能。但是,您建议的方法与伯纳德的方法相似。我得到了如上所示的结果。我附上了我正在努力实现的目标。感谢您的帮助。对于信息不足,我深表歉意。
    • @JohnnyAppleseed 乐于提供帮助。没关系,但我认为您可以投票支持帮助您找到解决方案的任何人,并将一个答案标记为可接受的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多