gpt4 book ai didi

android - 当我的列表为空时如何显示图像或文本?

转载 作者:行者123 更新时间:2023-11-29 13:51:44 24 4
gpt4 key购买 nike

我是 flutter 的新手,当我从 listView 中删除所有项目或 listView 为空时,我想显示像 No Record Found 这样的图像或文本。在 Android 中,我使用 setEmptyView() 来处理这种情况,但我不知道如何在 flutter 中执行此操作。目前我正在通过列表有人可以帮助我吗?

  DatabaseHelper databaseHelper = DatabaseHelper();
List<TaskModel> list;
int count = 0;
TaskModel taskModel;

@override
Widget build(BuildContext context) {
if (list == null) {
list = List<TaskModel>();
updateListView();
}
return Scaffold(
appBar: AppBar(
title: Text('Task'),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.purple,
child: Icon(Icons.add),
onPressed: () {
Navigator.push(context,
new MaterialPageRoute<void>(builder: (context) {
return new CreateTask(TaskModel('', '', '', '', '', '', ''),'Add Task');
}));
}),
body: getTasListView()
);
}

ListView getTasListView(){
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position){
return Card(
color: Colors.white,
elevation: 2.0,
child: Column(
children: <Widget>[
ListTile(
title: Text(this.list[position].task),
onLongPress: () => navigateToDetail(this.list[position],'Edit Task'),
)
],
),
);
}
);
}
}

最佳答案

检查长度或数组并决定返回哪个小部件。

   Widget getTasListView() {
return arrList.isNotEmpty
? ListView.builder(
itemCount: arrList.length,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: Column(
children: <Widget>[
ListTile(
title: Text(this.list[position].task),
onLongPress: () =>
navigateToDetail(this.list[position], 'Edit Task'),
)
],
),
);
})
: Center(child: Image.asset("path/image/img.png"));
}

并使用 arrList.length 而不是 itemCount: count

关于android - 当我的列表为空时如何显示图像或文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59404623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com