Last active
December 15, 2021 12:39
-
-
Save doyle-flutter/9a0c757229b7a9860be1822e2fec72b2 to your computer and use it in GitHub Desktop.
flutter_riverpod: ^1.0.2
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/material.dart'; | |
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
| void main() => runApp(ProviderScope(child: Sys())); | |
| class Sys extends StatelessWidget { | |
| const Sys({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) => MaterialApp(home: Main()); | |
| } | |
| class Main extends StatelessWidget { | |
| const Main({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return HomeView(); | |
| } | |
| } | |
| class HomeView extends StatelessWidget { | |
| const HomeView({Key? key,}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) => MainPage(); | |
| } | |
| class Logic extends StateNotifier<List<String>>{ | |
| Logic([List<String>? datas]) : super( datas ?? []); | |
| void add(String data) => state = [...state, data]; | |
| } | |
| StateNotifierProvider<Logic, List<String>> provider = StateNotifierProvider<Logic, List<String>>((ref) => Logic()); | |
| class MainPage extends ConsumerWidget{ | |
| @override | |
| Widget build(BuildContext context, WidgetRef ref) { | |
| final List<String> _datas = ref.watch(provider); | |
| final Logic _logic = ref.read<Logic>(provider.notifier); | |
| return Scaffold( | |
| appBar: AppBar( | |
| actions: [ | |
| IconButton( | |
| icon: Icon(Icons.watch), | |
| onPressed: () => Navigator.of(context).push( | |
| MaterialPageRoute(builder: (_) => Sub()) | |
| ), | |
| ) | |
| ], | |
| ), | |
| body: ListView.builder( | |
| itemCount: _datas.length, | |
| itemBuilder: (BuildContext context, int index) => ListTile( | |
| title: Text(_datas[index].toString()) // watch | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| child: Icon(Icons.add), | |
| onPressed: () => _logic.add('value'), // read | |
| ), | |
| ); | |
| } | |
| } | |
| class Sub extends StatelessWidget { | |
| const Sub({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return SubPage(); | |
| } | |
| } | |
| class SubPage extends ConsumerWidget{ | |
| @override | |
| Widget build(BuildContext context, WidgetRef ref) { | |
| final List<String> _datas = ref.watch<List<String>>(provider); | |
| final Logic _logic = ref.read<Logic>(provider.notifier); | |
| return Scaffold( | |
| appBar: AppBar(), | |
| body: Center(child: Text("watch : $_datas \n/ read : ${_logic.state}"),), | |
| floatingActionButton: FloatingActionButton( | |
| child: Icon(Icons.add_link), | |
| onPressed: () async{ | |
| await showDialog( | |
| context: context, | |
| builder: (context) => AlertDialog( | |
| title: Text("read ref .addDATA"), | |
| ) | |
| ); | |
| _logic.add("data"); | |
| }, | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment