简体   繁体   English

实现ListTile时颤振死代码Listview

[英]Flutter Dead code Listview while implementing ListTile

I would like to implement onTap in my ListView so I also need to implement return ListTile but I have a dead code on my ListTile snippet. 我想实现onTap在我ListView ,所以我还需要实现的回报ListTile但我有一个dead code在我ListTile片段。

I would like to know why and how to resolve it. 我想知道为什么以及如何解决它。

There is my code : 有我的代码:

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
        itemBuilder: (context, index) {
          var len = tasks.length;
          debugPrint("lenght: $len and index: $index");
          if(tasks.length - 1 == index) {
            _loadData(index);
            return Container(
                alignment: Alignment.center,
                padding: EdgeInsets.all(16.0),
                child: SizedBox(
                  width: 24.0,
                  height: 24.0,
                  child: CircularProgressIndicator(strokeWidth: 2.0),
                )
            );
          }
          else {
            var item = tasks[index];
            return Padding(
              padding: EdgeInsets.all(8.0),
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      getDotWidgetByType(item.taskStatus),
                      new Container(
                        padding: EdgeInsets.symmetric(horizontal: 8.0),
                        child: new Text(
                          item.taskTitle,
                          style: new TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                          ),
                        ),
                      ),
                      getStatusTextByType(item.taskStatus)
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 4.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(item.taskLeader),
                        Text(item.taskDeadLine),
                        IconButton(
                          onPressed: (){
                            //Todo change priorities/and status
                          },
                        icon: Icon(Icons.gps_fixed))
                      ],
                    ),
                  )
                ],
              ),
            );
          }            
          // DEAD CODE FROM HERE
          return ListTile(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => SingleTaskPage(taskItem: tasks[index]),
                ),
              );
            },
          );
          //TO HERE
        },
        separatorBuilder: (context, index) => Divider(height: 0.0),
        itemCount: tasks.length
    );
  }

Maybe there is another way to implement onTap but I have not find it. 也许还有另一种实现onTap的方法,但我没有找到它。

My snippet of ListTile come from : https://flutter.dev/docs/cookbook/navigation/passing-data 我的ListTile片段来自: https ://flutter.dev/docs/cookbook/navigation/passing-data

EDIT : I have this current UI : 编辑:我有这个当前的用户界面: 当前用户界面 I can totally change my code while I keep this UI. 保留此UI时,我可以完全更改代码。 My current problem is: I want to implement onTap() but I cannot with my current code logic. 我当前的问题是:我想实现onTap(),但无法使用当前的代码逻辑。

If your dead code simply is there for the onTap behaviour you could instead wrap the above within a GestureDetector . 如果您的死代码只是存在onTap行为,则可以将上述内容包装在GestureDetector

@override
Widget build(BuildContext context) {
  return ListView.separated(
      itemBuilder: (context, index) {
        var len = tasks.length;
        debugPrint("lenght: $len and index: $index");
        if(tasks.length - 1 == index) {
          _loadData(index);
          return Container(
              alignment: Alignment.center,
              padding: EdgeInsets.all(16.0),
              child: SizedBox(
                width: 24.0,
                height: 24.0,
                child: CircularProgressIndicator(strokeWidth: 2.0),
              )
          );
        }
        else {
          var item = tasks[index];
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => SingleTaskPage(taskItem: tasks[index]),
                ),
              );
            },
            child: Padding(
              padding: EdgeInsets.all(8.0),
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      getDotWidgetByType(item.taskStatus),
                      new Container(
                        padding: EdgeInsets.symmetric(horizontal: 8.0),
                        child: new Text(
                          item.taskTitle,
                          style: new TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                          ),
                        ),
                      ),
                      getStatusTextByType(item.taskStatus)
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 4.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(item.taskLeader),
                        Text(item.taskDeadLine),
                        IconButton(
                            onPressed: (){
                              //Todo change priorities/and status
                            },
                            icon: Icon(Icons.gps_fixed))
                      ],
                    ),
                  )
                ],
              ),
            ),
          );
        }
      },
      separatorBuilder: (context, index) => Divider(height: 0.0),
      itemCount: tasks.length
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM