Flutter:如何显示列表中的项目数

Bri*_* Oh 1 flutter

在学习 Flutter 的过程中,我编写了一个 CRUD 程序来显示项目列表。我想在屏幕底部显示列表中的项目数,但我一直无法实现。目前显示的代码包含(最后)一个BottomNavigationBar 和一个BottomNavigationBarItem,我试图在其中显示列表中的项目数,即:

                title: Text("Items = $this.itemCount")), // title: Text("")),
Run Code Online (Sandbox Code Playgroud)

但是它只显示“...”的项目数。我会很感激有人向我展示如何实现我的要求。


class NotesList extends StatefulWidget {
  @override
  NotesListPageState createState() => NotesListPageState();
}

class NotesListPageState extends State<NotesList> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('Notes List'),
          centerTitle: true,
        ),
        body: new Container(
          padding: new EdgeInsets.all(16.0),
          child: new FutureBuilder<List<Map>>(
            future: fetchDataFromDb(),
            builder: (context, snapshot) {
              if (snapshot == null) {
                return Container(
                  alignment: AlignmentDirectional.center,
                  child: CircularProgressIndicator(),
                );
              } else if (snapshot.hasData) {
                return ListView.builder(
                    itemCount: snapshot == null ? 0 : snapshot.data.length,
                    itemBuilder: (context, index) {
                      return Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            ListTile(
                                leading: (IconButton /* Edit */ (
                                    color: Colors.blue,
                                    icon: new Icon(Icons.edit),
                                    onPressed: () => _showEditScreen(
                                        Crud.eUpdate, snapshot.data[index]))),
                                onLongPress: () => _showEditScreen(
                                    Crud.eRead, snapshot.data[index]),
                                trailing: (IconButton(
                                    color: Colors.red,
                                    icon: new Icon(Icons.delete),
                                    onPressed: () => _showEditScreen(
                                        Crud.eDelete, snapshot.data[index])))),
                          ]);
                    });
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              } else {
                return new Text("No data in table");
              }
            },
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          onTap: (int index) {
            if (index == 1) {
              Navigator.of(context).pop();
            }
          },
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.info),
                title: Text("Items = $this.itemCount")), // title: Text("")),
            BottomNavigationBarItem(
              icon: Icon(Icons.add),
              title: Text('Create'),
            ),
          ],
        ));
  }
'''
Run Code Online (Sandbox Code Playgroud)

小智 5

在您的状态类中添加顶部

int count = 0;
Run Code Online (Sandbox Code Playgroud)

添加代码

    WidgetsBinding.instance
    .addPostFrameCallback((_) {
   setState(){
count = snapshot.data.length;
}
});
Run Code Online (Sandbox Code Playgroud)

在这两行之间

else if (snapshot.hasData) {
return ListView.builder(
Run Code Online (Sandbox Code Playgroud)

并将标题属性更改为

title: Text("Items = $count")),
Run Code Online (Sandbox Code Playgroud)