Last active
December 8, 2022 12:50
Revisions
-
biniama revised this gist
Dec 8, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ class MyHomePage extends StatelessWidget { ), body: Column( children: [ FavScreen(), ], ), ); @@ -63,7 +63,7 @@ class FavNotifier extends StateNotifier<List<String>> { } } class FavScreen extends ConsumerWidget { List<Map<String, String>> data = [{"id": '1', 'title': '1'},{"id": '2', 'title': '2'},{'id': '3', 'title': '3'},{'id': '4', 'title': '4'}]; @override -
biniama renamed this gist
Dec 8, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
biniama renamed this gist
Dec 8, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
biniama created this gist
Dec 8, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ //Add the following in `pubspec.yaml` dependencies: flutter_riverpod: ^2.1.1 import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; void main() => runApp(ProviderScope(child: MyApp())); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'RiverPod!', theme: ThemeData(primarySwatch: Colors.blue), home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('State Management with RiverPod!'), ), body: Column( children: [ Fav(), ], ), ); } } final favProvider = StateNotifierProvider<FavNotifier, List<String>>((ref) { return FavNotifier(); }); class FavNotifier extends StateNotifier<List<String>> { FavNotifier() : super([]); void add(String fav) { state = [...state, fav]; } void remove(String taskId) { state = [ for (final todo in state) if (todo != taskId) todo, ]; } void toggle(String taskId) { print('state $state'); print('taskId $taskId'); (state.contains(taskId)) ? remove(taskId) : add(taskId); print('state $state'); } } class Fav extends ConsumerWidget { List<Map<String, String>> data = [{"id": '1', 'title': '1'},{"id": '2', 'title': '2'},{'id': '3', 'title': '3'},{'id': '4', 'title': '4'}]; @override Widget build(BuildContext context, WidgetRef ref) { return Column( children: data.map((element) => FavItem(fav: element)).toList(), ); } } class FavItem extends ConsumerWidget { final Map<String, String> fav; FavItem({Key? key, required this.fav}) : super(key: key); @override Widget build(BuildContext context, WidgetRef ref) { List<String> favIds = ref.watch(favProvider); return Row( children: [ Checkbox( onChanged: (newValue) => ref.read(favProvider.notifier).toggle(fav['id']!), value: favIds.contains(fav['id']), ), Text(fav['title']!), ], ); } }