Created
June 25, 2020 13:52
-
-
Save HeavenOSK/74cb5eb0a3cc01d4d897198e15193986 to your computer and use it in GitHub Desktop.
This file contains 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/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_hooks/flutter_hooks.dart'; | |
import 'package:functional_widget_annotation/functional_widget_annotation.dart'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
import 'package:state_notifier/state_notifier.dart'; | |
part 'main_multi_owner.g.dart'; | |
class Counter extends StateNotifier<int> { | |
Counter() : super(0); | |
void increment() => state++; | |
} | |
final counterProvider = StateNotifierProvider<Counter>((ref) { | |
return Counter(); | |
}); | |
void main() { | |
runApp( | |
ProviderScope( | |
key: ValueKey('1st Scope'), | |
child: MyApp(), | |
), | |
); | |
} | |
@hwidget | |
Widget myApp() { | |
return MaterialApp( | |
routes: { | |
'/': (_) => FirstPage(), | |
'secondPage': (_) => SecondPageWithScope(), | |
}, | |
); | |
} | |
@hwidget | |
Widget firstPage() { | |
final context = useContext(); | |
return Scaffold( | |
body: Center( | |
child: RaisedButton( | |
onPressed: () { | |
Navigator.of(context).pushNamed('secondPage'); | |
}, | |
child: Text('Next Page'), | |
), | |
), | |
); | |
} | |
@hwidget | |
Widget secondPageWithScope() { | |
return ProviderScope( | |
key: ValueKey('2nd Scope'), | |
child: SecondPage(), | |
); | |
} | |
@hwidget | |
Widget secondPage() { | |
final counter = useProvider(counterProvider); | |
return Scaffold( | |
appBar: AppBar(title: Text('Second Page')), | |
body: Center( | |
child: Count(), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
counter.increment(); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
@hwidget | |
Widget count() { | |
final count = useProvider(counterProvider.state); | |
return Text(count.toString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment