I have a BottomNavigationBar in a stateless widget. I'm using ViewModelProvider to control the tab change when onTap event. I have no issue navigating using the BottomNavigationBar, but I was unable to control the navigation bar from inside the body. I'm using the following method, I have tried streambuilder to control the navigation, but streambuilder will dispose of the content when navigate to another tab which is not what I wanted. I am not able to click on button in Profile Page and navigate to Home page.
Below are my widget for BottomNavigationBar.
class BottomNavigationView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ViewModelProvider<BottomNavigationViewModel>.withConsumer(
viewModel: BottomNavigationViewModel(),
builder: (context, model, child) => Scaffold(
primary: false,
body: IndexedStack(
children: <Widget>[
BrowseView(),
HomeView(),
ProfileView(),
],
index: model.currentIndex,
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.white,
currentIndex: model.currentIndex,
onTap: model.changeView,
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.search),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: new Icon(WineIcon.person),
title: SizedBox.shrink(),
),
],
),
),
);
}
}
Navigation Bar View Model
class BottomNavigationViewModel extends ChangeNotifier {
int _currentIndex = 2;
int get currentIndex => _currentIndex;
void changeView(int index) {
_currentIndex = index;
notifyListeners();
}
}
Profile View & Profile View Model
class ProfileView extends StatelessWidget {
const ProfileView ({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ViewModelProvider<ProfileViewModel>.withConsumer(
viewModel: ProfileViewModel(),
builder: (context, model, child) => Scaffold(
body: Center(
child: RaisedButton(
onPressed: ()=> model.goHome(),
child:Text('Go to Home'),
),
)
),
);
}
}
class ProfileViewModel extends ChangeNotifier {
final BottomNavigationViewModel _bottomNavigationBar =
locator<BottomNavigationViewModel>();
void goHome() {
_bottomNavigationBar.changeView(1);
}
}