Created
March 27, 2022 06:55
-
-
Save malviyaritesh/47e6bfc2b212f43f6d39b5fe39fbb35b to your computer and use it in GitHub Desktop.
CupertinoTabBar and BottomNavigationBarItem UI not updating with ChangeNotifierProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:provider/provider.dart'; | |
void main() { | |
runApp(const App()); | |
} | |
class App extends StatelessWidget { | |
const App({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return CupertinoApp( | |
theme: const CupertinoThemeData( | |
brightness: Brightness.light, | |
), | |
home: ChangeNotifierProvider( | |
create: (context) => AppState(), | |
child: const TabsPage(), | |
), | |
); | |
} | |
} | |
class TabsPage extends StatelessWidget { | |
const TabsPage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Consumer<AppState>( | |
builder: (context, appState, child) { | |
return CupertinoTabScaffold( | |
tabBar: CupertinoTabBar( | |
backgroundColor: CupertinoColors.white, | |
currentIndex: appState.currentTabIndex, | |
onTap: (index) => appState.currentTabIndex = index, | |
items: const [ | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.house), | |
activeIcon: Icon(CupertinoIcons.house_fill), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.play_rectangle), | |
activeIcon: Icon(CupertinoIcons.play_rectangle_fill), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.heart), | |
activeIcon: Icon(CupertinoIcons.heart_fill), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.music_albums), | |
activeIcon: Icon(CupertinoIcons.music_albums_fill), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.ellipsis_circle), | |
activeIcon: Icon(CupertinoIcons.ellipsis_circle_fill), | |
), | |
], | |
), | |
tabBuilder: (context, index) { | |
return const TabPage(); | |
}, | |
); | |
}, | |
); | |
} | |
} | |
class TabPage extends StatelessWidget { | |
const TabPage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return CupertinoTabView( | |
builder: (context) => Consumer<AppState>( | |
builder: (context, appState, child) => Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text('Current Tab: ${appState.currentTabIndex}'), | |
const SizedBox(height: 8.0), | |
...List.generate( | |
5, | |
(index) => Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: CupertinoButton.filled( | |
child: Text('Go to tab $index'), | |
onPressed: () => appState.currentTabIndex = index, | |
), | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class AppState extends ChangeNotifier { | |
int _currentTabIndex = 0; | |
int get currentTabIndex => _currentTabIndex; | |
set currentTabIndex(int index) { | |
_currentTabIndex = index; | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
View this SO answer for solution: https://stackoverflow.com/a/71636103/7621577