Advertisement
mrhumbility

Riverpod Select Test

Jul 14th, 2021
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.85 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3.  
  4. void main() {
  5.   runApp(
  6.     ProviderScope(
  7.       child: MyApp(),
  8.     ),
  9.   );
  10. }
  11.  
  12. class MyApp extends StatelessWidget {
  13.   @override
  14.   Widget build(BuildContext context) {
  15.     return MaterialApp(
  16.       debugShowCheckedModeBanner: false,
  17.       title: 'riverpod test',
  18.       theme: ThemeData(
  19.         brightness: Brightness.dark,
  20.         primarySwatch: Colors.blue,
  21.       ),
  22.       home: APage(),
  23.     );
  24.   }
  25. }
  26.  
  27. class APage extends StatelessWidget {
  28.   @override
  29.   Widget build(BuildContext context) {
  30.     return Consumer(builder: (context, ref, child) {
  31.       return Scaffold(
  32.         appBar: AppBar(
  33.           title: Text("Riverpod .select() Test"),
  34.         ),
  35.         body: Column(
  36.           mainAxisAlignment: MainAxisAlignment.center,
  37.           children: [
  38.             Row(
  39.               mainAxisAlignment: MainAxisAlignment.center,
  40.               children: [
  41.                 Consumer(
  42.                   builder: (context, ref, child) {
  43.                     final color = ref.watch(colorProviderState.select((value) => value.one));
  44.                     return Container(
  45.                       color: color,
  46.                       child: SizedBox(width: 50.0, height: 50.0),
  47.                     );
  48.                   },
  49.                 ),
  50.                 SizedBox(width: 15.0),
  51.                 Consumer(
  52.                   builder: (context, ref, child) {
  53.                     final color = ref.watch(colorProviderState.select((value) => value.two));
  54.                     return Container(
  55.                       color: color,
  56.                       child: SizedBox(width: 50.0, height: 50.0),
  57.                     );
  58.                   },
  59.                 ),
  60.               ],
  61.             ),
  62.             SizedBox(height: 25.0),
  63.             Row(
  64.               mainAxisAlignment: MainAxisAlignment.center,
  65.               children: [
  66.                 ElevatedButton(
  67.                   onPressed: () {
  68.                     ref.read(colorProviderState.notifier).changeOne();
  69.                   },
  70.                   child: Icon(Icons.login),
  71.                 ),
  72.                 SizedBox(width: 15.0),
  73.                 ElevatedButton(
  74.                   onPressed: () {
  75.                     ref.read(colorProviderState.notifier).changeTwo();
  76.                   },
  77.                   child: Icon(Icons.delete),
  78.                 ),
  79.               ],
  80.             ),
  81.           ],
  82.         ),
  83.       );
  84.     });
  85.   }
  86. }
  87.  
  88. final colorProviderState = StateNotifierProvider((ref) => ColorManagerState());
  89.  
  90. class ColorManagerState extends StateNotifier<MyColors> {
  91.   ColorManagerState() : super(MyColors());
  92.   MyColors myColors = MyColors();
  93.  
  94.   void changeOne() {
  95.     print("State one: ${myColors.one}");
  96.     myColors.one = Colors.cyan;
  97.     print("State one: ${myColors.one}");
  98.   }
  99.  
  100.   void changeTwo() {
  101.     print("State one: ${myColors.two}");
  102.     myColors.two = Colors.green;
  103.     print("State one: ${myColors.two}");
  104.   }
  105. }
  106.  
  107. class MyColors {
  108.   Color _one = Colors.black;
  109.   Color _two = Colors.black;
  110.  
  111.   Color get one => _one;
  112.   Color get two => _two;
  113.   set one(Color newClr) => newClr;
  114.   set two(Color newClr) => newClr;
  115. }
  116.  
  117. // ------------------------------------------------
  118. // ------------------------------------------------
  119. // ------------------------------------------------
  120.  
  121. final colorProviderChange = ChangeNotifierProvider((ref) => ColorManagerChange());
  122.  
  123. class ColorManagerChange extends ChangeNotifier {
  124.   Color _one = Colors.black;
  125.   Color _two = Colors.black;
  126.  
  127.   Color get one => _one;
  128.   Color get two => _two;
  129.  
  130.   void changeOne() {
  131.     print("Change one: $_one");
  132.     _one = Colors.yellow;
  133.     print("Change one: $_one");
  134.   }
  135.  
  136.   void changeTwo() {
  137.     print("Change two: $_two");
  138.     _two = Colors.pink;
  139.     print("Change two: $_two");
  140.   }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement