switched to flutter_riverpod for state/page management.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import 'package:app/pages/settings_widget.dart';
|
import 'package:app/pages/settings_widget.dart';
|
||||||
import 'package:app/pages/conversations_list_widget.dart';
|
import 'package:app/pages/conversations_list_widget.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import 'package:app/theme.dart';
|
import 'package:app/theme.dart';
|
||||||
@@ -37,19 +38,19 @@ BottomNavigationBarItem _buildNavigationItem(int index) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// main.dart
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(
|
runApp(
|
||||||
ChangeNotifierProvider<MyState>(
|
ProviderScope(
|
||||||
create: (_) => MyState(),
|
|
||||||
child: MyApp(),
|
child: MyApp(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends ConsumerWidget {
|
||||||
MyApp({super.key});
|
MyApp({super.key});
|
||||||
|
|
||||||
|
final selectedIndexProvider = StateProvider<int>((_) => 0); // Initial state
|
||||||
|
|
||||||
// This list holds the pages to navigate between
|
// This list holds the pages to navigate between
|
||||||
final List<Widget> _pages = [
|
final List<Widget> _pages = [
|
||||||
// Replace with the widget for your first page (e.g., HomeScreen())
|
// 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.
|
// This widget is the root of your application.
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
int selectedIndex = Provider.of<MyState>(context).selectedIndex;
|
final selectedIndex = ref.watch(selectedIndexProvider);
|
||||||
|
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'App',
|
title: 'App',
|
||||||
@@ -78,12 +79,12 @@ class MyApp extends StatelessWidget {
|
|||||||
centerTitle: false,
|
centerTitle: false,
|
||||||
elevation: 0,
|
elevation: 0,
|
||||||
),
|
),
|
||||||
body: _pages[Provider.of<MyState>(context, listen: false).selectedIndex],
|
body: _pages[selectedIndex],
|
||||||
bottomNavigationBar: BottomNavigationBar(
|
bottomNavigationBar: BottomNavigationBar(
|
||||||
// Set the number of items to the length of the pages list
|
// Set the number of items to the length of the pages list
|
||||||
items: List.generate(_pages.length, (index) => _buildNavigationItem(index)),
|
items: List.generate(_pages.length, (index) => _buildNavigationItem(index)),
|
||||||
currentIndex: selectedIndex,
|
currentIndex: selectedIndex,
|
||||||
onTap: (index) => Provider.of<MyState>(context, listen: false).selectedIndex = index, // Update using Provider
|
onTap: (index) => ref.read(selectedIndexProvider.notifier).state = index,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -326,6 +326,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.20"
|
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:
|
flutter_shaders:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -800,6 +808,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.8"
|
version: "0.2.8"
|
||||||
|
riverpod:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: riverpod
|
||||||
|
sha256: f21b32ffd26a36555e501b04f4a5dca43ed59e16343f1a30c13632b2351dfa4d
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.5.1"
|
||||||
rxdart:
|
rxdart:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -933,6 +949,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.11.1"
|
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:
|
stop_watch_timer:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ dependencies:
|
|||||||
provider: any
|
provider: any
|
||||||
easy_debounce: any
|
easy_debounce: any
|
||||||
video_player: ^2.9.1
|
video_player: ^2.9.1
|
||||||
|
flutter_riverpod: ^2.5.1
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|||||||
Reference in New Issue
Block a user