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