【问题标题】:Flutter how to refresh appBar Title after FutureBuilderFlutter 如何在 FutureBuilder 之后刷新 appBar Title
【发布时间】:2021-10-23 06:23:57
【问题描述】:

是否可以在小部件 FutureBuilder 之后刷新 appBar 标题? 我想在 FutureBuilder 完成后设置标题

class _SimpleBarChart extends State<SimpleBarChartPage> {
  String _appBarTitle = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(_appBarTitle)),
        body: Center(child: futureCAGraph()));
  }

  futureCAGraph() {
    return FutureBuilder(
        future: BddAMKGraphCA().getDataGraphCA(_caAnnee),
        builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, int currentIndex) {
                  return affGraphCA(context);
                });
          } else if (snapshot.hasError) {
            return Text('${snapshot.error}');
          }
          //return  a circular progress indicator.
          return new CircularProgressIndicator();
        });
  }

【问题讨论】:

    标签: flutter appbar flutter-futurebuilder


    【解决方案1】:

    从未来获取数据后,您可以使用 setstate() 轻松更新它。只需在 setstate() 中分配 _appBarTitle 如下所示,

     setState(() {
      _appBarTitle=//assign your snapshot data
      });
    

    【讨论】:

      【解决方案2】:

      在构建方法期间调用setState 不是一个好习惯。相反,您可以像这样在构建器中移动Scaffold

        Widget build(BuildContext context) {
          return FutureBuilder(
              future: BddAMKGraphCA().getDataGraphCA(_caAnnee),
              builder: (context, AsyncSnapshot<List<dynamic>> snapshot) 
                => Scaffold(
                  appBar: AppBar(title: Text(_appBarTitle)),
                  body: Center(child: (snapshot.hasData) 
                   ? ListView.builder(
                      itemCount: snapshot.data!.length,
                      itemBuilder: (context, int currentIndex) {
                        return affGraphCA(context);
                      }) 
                   : (snapshot.hasError) 
                         ? Text('${snapshot.error}')
                         : //return  a circular progress indicator.
                          CircularProgressIndicator(),
                  ),
                )
              );
        }
      

      【讨论】:

        【解决方案3】:

        抱歉,测试 appBar 的标题后不打印任何内容

        Widget build(BuildContext context) {
            return FutureBuilder(
                future: BddAMKGraphCA().getDataGraphCA(_caAnnee),
                builder: (context, AsyncSnapshot<List<dynamic>> snapshot) 
                  => Scaffold(
                    appBar: AppBar(title: Text(_appBarTitle)),
                    body: Center(child: (snapshot.hasData) 
                     ? ListView.builder(
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, int currentIndex) {
                        **print('appBar something');**
                          return affGraphCA(context);
                        }) 
                     : (snapshot.hasError) 
                           ? Text('${snapshot.error}')
                           : //return  a circular progress indicator.
                            CircularProgressIndicator(),
                    ),
                  )
                );
          }
        

        【讨论】:

          猜你喜欢
          • 2021-04-06
          • 2021-05-04
          • 1970-01-01
          • 2020-05-24
          • 2020-09-28
          • 2021-11-29
          • 1970-01-01
          • 1970-01-01
          • 2021-04-19
          相关资源
          最近更新 更多