实现 ListTile 时 Flutter Dead code Listview
Flutter Dead code Listview while implementing ListTile
我想在我的 ListView
中实现 onTap
所以我还需要实现 return ListTile
但我的 dead code
=13=] 片段。
我想知道为什么以及如何解决。
这是我的代码:
@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
编辑: 我有这个电流 UI:
我可以在保留此 UI 的同时完全更改我的代码。
我目前的问题是:我想实现 onTap() 但我不能用我当前的代码逻辑。
如果你的死代码只是为了 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
);
}
我想在我的 ListView
中实现 onTap
所以我还需要实现 return ListTile
但我的 dead code
=13=] 片段。
我想知道为什么以及如何解决。
这是我的代码:
@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
编辑: 我有这个电流 UI:
如果你的死代码只是为了 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
);
}