switched to flutter_riverpod for state/page management.

This commit is contained in:
Joshua
2024-07-06 15:37:40 -06:00
parent 766591546f
commit 1d5bf55eb9
3 changed files with 34 additions and 8 deletions
+9 -8
View File
@@ -1,6 +1,7 @@
import 'package:app/pages/settings_widget.dart';
import 'package:app/pages/conversations_list_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:provider/provider.dart';
import 'package:app/theme.dart';
@@ -37,19 +38,19 @@ BottomNavigationBarItem _buildNavigationItem(int index) {
);
}
// main.dart
void main() {
runApp(
ChangeNotifierProvider<MyState>(
create: (_) => MyState(),
ProviderScope(
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
class MyApp extends ConsumerWidget {
MyApp({super.key});
final selectedIndexProvider = StateProvider<int>((_) => 0); // Initial state
// This list holds the pages to navigate between
final List<Widget> _pages = [
// Replace with the widget for your first page (e.g., HomeScreen())
@@ -60,8 +61,8 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
int selectedIndex = Provider.of<MyState>(context).selectedIndex;
Widget build(BuildContext context, WidgetRef ref) {
final selectedIndex = ref.watch(selectedIndexProvider);
return MaterialApp(
title: 'App',
@@ -78,12 +79,12 @@ class MyApp extends StatelessWidget {
centerTitle: false,
elevation: 0,
),
body: _pages[Provider.of<MyState>(context, listen: false).selectedIndex],
body: _pages[selectedIndex],
bottomNavigationBar: BottomNavigationBar(
// Set the number of items to the length of the pages list
items: List.generate(_pages.length, (index) => _buildNavigationItem(index)),
currentIndex: selectedIndex,
onTap: (index) => Provider.of<MyState>(context, listen: false).selectedIndex = index, // Update using Provider
onTap: (index) => ref.read(selectedIndexProvider.notifier).state = index,
),
),
);
+24
View File
@@ -326,6 +326,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.20"
flutter_riverpod:
dependency: "direct main"
description:
name: flutter_riverpod
sha256: "0f1974eff5bbe774bf1d870e406fc6f29e3d6f1c46bd9c58e7172ff68a785d7d"
url: "https://pub.dev"
source: hosted
version: "2.5.1"
flutter_shaders:
dependency: transitive
description:
@@ -800,6 +808,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.8"
riverpod:
dependency: transitive
description:
name: riverpod
sha256: f21b32ffd26a36555e501b04f4a5dca43ed59e16343f1a30c13632b2351dfa4d
url: "https://pub.dev"
source: hosted
version: "2.5.1"
rxdart:
dependency: transitive
description:
@@ -933,6 +949,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.11.1"
state_notifier:
dependency: transitive
description:
name: state_notifier
sha256: b8677376aa54f2d7c58280d5a007f9e8774f1968d1fb1c096adcb4792fba29bb
url: "https://pub.dev"
source: hosted
version: "1.0.0"
stop_watch_timer:
dependency: transitive
description:
+1
View File
@@ -44,6 +44,7 @@ dependencies:
provider: any
easy_debounce: any
video_player: ^2.9.1
flutter_riverpod: ^2.5.1
dev_dependencies:
flutter_test:
sdk: flutter