实施ListTile时颤振死代码Listview

时间:2019-04-16 07:01:49

标签: listview flutter dead-code

我想在自己的onTap中实现ListView,所以我还需要实现return ListTile,但是我的dead code片段中有一个ListTile

我想知道为什么以及如何解决它。

有我的代码:

  @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
    );
  }

也许还有另一种实现onTap的方法,但我没有找到它。

我的ListTile片段来自:https://flutter.dev/docs/cookbook/navigation/passing-data

编辑::我具有当前的用户界面:current UI 保留此UI时,我可以完全更改代码。 我当前的问题是:我想实现onTap(),但是无法使用当前的代码逻辑。

1 个答案:

答案 0 :(得分:1)

如果您的死代码只是用于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
  );
}