In flutter, if we use value via multiple class using constructor, it will be better to use provider. This provider is a little bit tricky but it makes sense to me. I'll share necessary function here.
1.Create class using StateNotifier. In generic, define what data you want to provide, this time >\
2.The constructor is class_name() : super([]); The parenthesis of super is [] because shared data is List type.\
3.The shared data is including in state, so if you want to manipulate shared data, use state in function. For example, state = state.where((m) => m.id != meal.id).toList(); \
4.Finally, you need to define provider which returns Notifier \
The whole code looks like below: \
\
import 'package:flutter_riverpod/flutter_riverpod.dart'; \
import 'package:meals/models/meal.dart';
\
\
class FavoriteMealsNotifier extends StateNotifier> {
FavoriteMealsNotifier() : super([]);
bool toggleMealFavoriteStatus(Meal meal) {
final mealIsFavorite = state.contains(meal);
if (mealIsFavorite) {
state = state.where((m) => m.id != meal.id).toList();
return false;
} else {
state = [...state, meal];
return true;
}
}
}
\
final favoriteMealsProvider =
StateNotifierProvider>((ref) { \
return FavoriteMealsNotifier(); \
});
This is the provider, you need to set other classes to ConsumerWidget if stateless and ConsumerStatefulWidget if stateful.
To show new page when user tap, use Navigator.of(context).push(MaterialPageRoute(builder: builder)). In this builder, it is required to provide (ctx) = {} function and inside of this blacket, you need to add what you want to show on the page.
When we know the length of list, it's recommended using ListView widget. This widget is used with builder function and the attributions are itemCount and itemBuilder. For itemBuilder, you can utilise Dismissible widget. This widget is used with onDismissed which is method, when user slides the item, it is executed. This time, remove function has been provided and child. This child comes with ExpenseItem.
To provide theme for flutter app, apply theme in MaterialApp widget. theme needs ThemeData widget to set theme and useMaterial3, colorScheme and appBarTheme, and so on. When user slide left or right to delete item, red background is convenient to see because it lets user know, it will be deleted, so to apply this, in Dismissible widget, you need to apply background with Container widget. For color attribution, you need to provide Theme.of(context).colorScheme.error.