diff --git a/lib/data/appProviders/preferences_provider.dart b/lib/data/appProviders/preferences_provider.dart index ef188a0..de440ef 100644 --- a/lib/data/appProviders/preferences_provider.dart +++ b/lib/data/appProviders/preferences_provider.dart @@ -106,13 +106,16 @@ class PreferencesProvider { // --------------------------------------------------------- static const String _futureEventsKey = 'futureEvents'; + static final ValueNotifier futureEventsNotifier = ValueNotifier(false); - static Future get futureEvents async { + static Future loadFutureEvents() async { final prefs = await SharedPreferences.getInstance(); - return prefs.getBool(_futureEventsKey) ?? false; // Default + bool value = prefs.getBool(_futureEventsKey) ?? false; // Default + futureEventsNotifier.value = value; } static Future setFutureEvents(bool value) async { + futureEventsNotifier.value = value; final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_futureEventsKey, value); } diff --git a/lib/pages/events_page.dart b/lib/pages/events_page.dart index 2346033..249d8ad 100644 --- a/lib/pages/events_page.dart +++ b/lib/pages/events_page.dart @@ -20,7 +20,6 @@ class EventsPage extends StatefulWidget { class _EventsPageState extends State { late bool _isDayView = true; - late bool _futureEvents = true; @override void initState() { @@ -33,63 +32,64 @@ class _EventsPageState extends State { bool value = await PreferencesProvider.isDayView; setState(() { _isDayView = value; - }); // Trigger a rebuild after updating _isDayView + }); } Future _loadEventListFilter() async { - bool value = await PreferencesProvider.futureEvents; - setState(() { - _futureEvents = value; - }); // Trigger a rebuild after updating _isDayView + await PreferencesProvider.loadFutureEvents(); } @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('Events'), - automaticallyImplyLeading: false, - backgroundColor: Theme - .of(context) - .appBarTheme - .backgroundColor, - foregroundColor: Theme - .of(context) - .appBarTheme - .foregroundColor, - actions: [ - _isDayView ? const SizedBox.shrink() : - IconButton( - icon: Icon(_futureEvents ? Icons.filter_alt : Icons.filter_alt_off), - color: Theme + return ValueListenableBuilder( + valueListenable: PreferencesProvider.futureEventsNotifier, + builder: (context, futureEvents, child) { + return Scaffold( + appBar: AppBar( + title: const Text('Events'), + automaticallyImplyLeading: false, + backgroundColor: Theme .of(context) - .colorScheme - .onSurface, - onPressed: () { - setState(() { - _futureEvents = !_futureEvents; - PreferencesProvider.setFutureEvents(_futureEvents); - }); - }, - tooltip: 'Filter only future events', - ), - IconButton( - icon: Icon(_isDayView ? Icons.list : Icons.calendar_today), - color: Theme + .appBarTheme + .backgroundColor, + foregroundColor: Theme .of(context) - .colorScheme - .onSurface, - onPressed: () { - setState(() { - _isDayView = !_isDayView; - PreferencesProvider.setIsDayView(_isDayView); - }); - }, + .appBarTheme + .foregroundColor, + actions: [ + _isDayView ? const SizedBox.shrink() : + IconButton( + icon: Icon( + futureEvents ? Icons.filter_alt : Icons.filter_alt_off), + color: Theme + .of(context) + .colorScheme + .onSurface, + onPressed: () { + futureEvents = !futureEvents; + PreferencesProvider.setFutureEvents(futureEvents); + }, + tooltip: 'Filter only future events', + ), + IconButton( + icon: Icon(_isDayView ? Icons.list : Icons.calendar_today), + color: Theme + .of(context) + .colorScheme + .onSurface, + onPressed: () { + setState(() { + _isDayView = !_isDayView; + PreferencesProvider.setIsDayView(_isDayView); + }); + }, + ), + ], ), - ], - ), - body: _isDayView ? DayViewCalendar() : EventList( - futureEvents: _futureEvents), + body: _isDayView ? DayViewCalendar() : EventList( + futureEvents: futureEvents), + ); + }, ); } } @@ -227,14 +227,14 @@ class EventList extends StatefulWidget { } class EventListState extends State { - late bool _futureEvents = false; - @override - void didUpdateWidget(EventList oldWidget) { - super.didUpdateWidget(oldWidget); - if (oldWidget.futureEvents != widget.futureEvents) { - _futureEvents = widget.futureEvents; // Update local variable if futureEvents changed - } + void initState() { + super.initState(); + _loadPreferences(); + } + + Future _loadPreferences() async { + PreferencesProvider.loadFutureEvents(); } @override @@ -250,21 +250,27 @@ class EventListState extends State { // ] // ), Expanded( // Use Expanded to allow ListView.builder to take available space - child: Consumer( // Wrap ListView.builder with Consumer - builder: (context, itemList, child) { - List items; - if (_futureEvents) { - items = itemList.filterPastEvents(); - } else { - items = itemList.items(); + child: ValueListenableBuilder( + valueListenable: PreferencesProvider.futureEventsNotifier, + builder: (context, builderValue, child) { + return Consumer< + EventsProvider>( // Wrap ListView.builder with Consumer + builder: (context, itemList, child) { + List items; + if (builderValue) { + items = itemList.filterPastEvents(); + } else { + items = itemList.items(); + } + return ListView.builder( + itemCount: items.length, + itemBuilder: (context, index) { + return EventListTile(item: items[index]); + }, + ); + }, + ); } - return ListView.builder( - itemCount: items.length, - itemBuilder: (context, index) { - return EventListTile(item: items[index]); - }, - ); - }, ), ), ],