0

I'm looking for ways to update a Text widget in a Scaffold from a body linked to a bottomNavigationBar.

The Text widget contains a status message ("The weather is nice today). The bottomNavigationBar has 3 items. Only one of them has a RefreshIndicator that fetches the latest weather information, which should be used to update the status message. I can't accomplish this because I don't know how to update the message in the Text widget from the child page in bottomNavigationBar.

Here is my file containing the Text widget and the bottomNavigationBar:

...
final List<BottomNavigationBarItem> bottomTabs = [
  new BottomNavigationBarItem(
      icon: new Icon(CupertinoIcons.home), title: new Text('Home')),
  new BottomNavigationBarItem(
      icon: new Icon(CupertinoIcons.book), title: new Text('Contact')),
  new BottomNavigationBarItem(
      icon: new Icon(CupertinoIcons.settings), title: new Text('Settings'))
];
...
  int _currentIndex = 0;

  final List<Widget> _children = [
    new HomeRoute(),
    new ContactRoute(),
    new SettingsRoute()
  ];
...
@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.add),
                  onPressed: () {},
                )
              ],
              expandedHeight: 200.0,
              elevation: 0.0,
              floating: false,
              pinned: true,
              flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: Text("Weather Status",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.0,
                      )),
                  background: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(_overallBusStatus,
                          style:
                              TextStyle(color: Colors.white, fontSize: 16.0)),
                    ],
                  )),
            ),
          ];
        },
        body: _children[_currentIndex], // new
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped, // new
        currentIndex: _currentIndex, // new
        items: bottomTabs,
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

And the content of HomeRoute:

...
  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new RefreshIndicator(
        child: new ListView(
          children: _getCards(),
          padding: EdgeInsets.all(0.0),
        ),
        onRefresh: _refreshHome,
      ),
    );
  }
...
  Future<void> _refreshHome() async {
    print('Refreshing...');
    Future future = getWeather();
    future.then((result) => updateWeatherStatus(result));
  }

Thank you, Joseph

Joseph Lam
  • 5,289
  • 3
  • 14
  • 13
  • I believe you should use Redux instead of setstate, Redux let you 'refresh' any widget at anytime. e.g. a widget display the whether info, and you have a online socket.io connection to a 'whether server', when this server send updated info to the app, the widget will be refreshed automatically, without having any user interaction. let me know if you have problem using Redux. – Kenneth Li Feb 26 '19 at 05:01
  • Possible duplicate of [Emit the data to parent Widget in Flutter](https://stackoverflow.com/questions/51463906/emit-the-data-to-parent-widget-in-flutter) – Willy Mar 01 '19 at 15:39

0 Answers0