Skip to content

Instantly share code, notes, and snippets.

@davidmartos96
Last active February 18, 2020 16:14
Show Gist options
  • Save davidmartos96/5e8e4e0c807ee1c7ee4cb64597c1cb78 to your computer and use it in GitHub Desktop.
Save davidmartos96/5e8e4e0c807ee1c7ee4cb64597c1cb78 to your computer and use it in GitHub Desktop.
ChangeNotifierProxyProvider update
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MaterialApp(home: MainPage()));
}
const List<Destination> allDestinations = <Destination>[
Destination(0, 'Home', Icons.home),
Destination(1, 'Other', Icons.settings),
];
class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage>
with TickerProviderStateMixin<MainPage> {
List<GlobalKey> _destinationKeys;
List<GlobalKey<NavigatorState>> _navigatorKeys;
int _currentIndex = 0;
@override
void initState() {
super.initState();
_destinationKeys = List<GlobalKey>.generate(
allDestinations.length, (int index) => GlobalKey()).toList();
_navigatorKeys = List<GlobalKey<NavigatorState>>.generate(
allDestinations.length, (int index) => GlobalKey()).toList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: allDestinations.map((Destination destination) {
final Widget view = KeyedSubtree(
key: _destinationKeys[destination.index],
child: Navigator(
key: _navigatorKeys[destination.index],
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute(
settings: settings,
builder: (BuildContext context) {
if (destination.index == 0) {
return Home();
} else {
return OtherPage();
}
},
);
},
),
);
return Offstage(
child: view,
offstage: destination.index != _currentIndex,
);
}).toList(),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
items: allDestinations.map((Destination destination) {
return BottomNavigationBarItem(
icon: Icon(destination.icon),
title: Text(destination.title),
);
}).toList(),
),
);
}
}
class Home extends StatefulWidget {
Home({Key key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<int>.value(
value: 0,
),
ChangeNotifierProxyProvider<int, MyNotifier>(
create: (context) {
return MyNotifier();
},
update: (BuildContext context, int value, MyNotifier previous) {
print("update called: ${DateTime.now()}");
return previous;
},
),
],
child: Scaffold(
body: Builder(builder: (context) {
final notifier = Provider.of<MyNotifier>(context);
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("MyNotifier ${notifier.hashCode}"),
OutlineButton(
child: Text("setState runs update"),
onPressed: () {
setState(() {});
},
),
],
),
);
}),
),
);
}
}
class MyNotifier extends ChangeNotifier {}
class OtherPage extends StatelessWidget {
const OtherPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text("Other page")),
);
}
}
class Destination {
const Destination(this.index, this.title, this.icon);
final int index;
final String title;
final IconData icon;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment