列表为空时如何显示图像或文本?

时间:2019-12-19 06:55:48

标签: android ios flutter dart flutter-layout

我是个陌生的人,当我从listView删除所有项目或listView为空时,我想显示类似No Record Found的图像或文本。在Android中,我使用setEmptyView()来解决这种情况,但是我不知道该如何应对。目前我正在通过List,有人可以帮我吗?

  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'),
                )
              ],
            ),
          );
        }
    );
  }
}

2 个答案:

答案 0 :(得分:0)

检查长度或数组并确定要返回的小部件。

   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

答案 1 :(得分:0)

概述:我将SourceClass的列表映射到名为Cards的MyCard类列表中。 MyCard运行Card小部件,并在其中使用SourceClass字段。我检查卡的长度,以确定是否应显示“未显示找到记录的图像或消息”

@override
Widget build(BuildContext context) {

List<Widget> cards = mylist
    ?.map((mylist) =>
        MyCard(list: listSourceClass))
    ?.toList();    

return new Scaffold(
    key: this._scaffoldKey,
    appBar: setAppBar(
        this._scaffoldKey,
        context,
        "abc"),
    body: cards.length == 0
        ? Text("No Records Found",style:NoRecordsFoundStyle)
        : ListView.builder(
            itemCount: cards?.length,
            itemBuilder: (BuildContext context, int index) {
              return cards[index];
            }),