Created
December 18, 2024 20:47
-
-
Save hectorAguero/f2ada3eb26813be7fda40eb3ff5ea0d0 to your computer and use it in GitHub Desktop.
Months of the Year using InheritedNotifier
This file contains 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 characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: CalendarWidget( | |
selectedDate: DateTime.now(), | |
), | |
), | |
), | |
); | |
} | |
} | |
class CalendarWidget extends StatelessWidget { | |
const CalendarWidget({super.key, required this.selectedDate}); | |
final DateTime selectedDate; | |
@override | |
Widget build(BuildContext context) { | |
final initialMonth = MonthsOfTheYear.values[selectedDate.month - 1]; | |
// Initialize the ValueNotifier with the default month | |
return CalendarProvider( | |
notifier: ValueNotifier<MonthsOfTheYear>(initialMonth), | |
child: Scaffold( | |
appBar: AppBar(title: Text('Calendar')), | |
body: Center( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
MonthNavigation(), | |
// A child context is needed to access the provider | |
// can be a extracted widget or a builder | |
Builder( | |
builder: (context) => Text( | |
'Selected Month: ${CalendarProvider.of(context).name}', | |
style: TextStyle(fontSize: 20), | |
), | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class MonthNavigation extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
mainAxisSize: MainAxisSize.min, | |
spacing: 20, | |
children: [ | |
IconButton.filled( | |
icon: Icon(Icons.arrow_left), | |
color: Colors.red, | |
onPressed: () { | |
final currentMonth = CalendarProvider.of(context); | |
final newMonth = MonthsOfTheYear.values[ | |
(currentMonth.index - 1 + MonthsOfTheYear.values.length) % | |
MonthsOfTheYear.values.length]; | |
CalendarProvider.updateMonth(context, newMonth); | |
}, | |
), | |
IconButton.filledTonal( | |
icon: Icon(Icons.arrow_right), | |
color: Colors.red, | |
onPressed: () { | |
final currentMonth = CalendarProvider.of(context); | |
final newMonth = MonthsOfTheYear.values[ | |
(currentMonth.index + 1) % MonthsOfTheYear.values.length]; | |
CalendarProvider.updateMonth(context, newMonth); | |
}, | |
), | |
], | |
); | |
} | |
} | |
// Define your enum for the months | |
enum MonthsOfTheYear { | |
january, | |
february, | |
march, | |
april, | |
may, | |
june, | |
july, | |
august, | |
september, | |
october, | |
november, | |
december; | |
} | |
// Create the InheritedNotifier to hold the selected month | |
class CalendarProvider | |
extends InheritedNotifier<ValueNotifier<MonthsOfTheYear>> { | |
const CalendarProvider({ | |
Key? key, | |
required ValueNotifier<MonthsOfTheYear> notifier, | |
required Widget child, | |
}) : super(key: key, notifier: notifier, child: child); | |
// Helper method to access the current month | |
static MonthsOfTheYear of(BuildContext context) { | |
final widget = | |
context.dependOnInheritedWidgetOfExactType<CalendarProvider>(); | |
assert(widget != null, 'No CalendarProvider found in context'); | |
return widget!.notifier!.value; | |
} | |
// Helper method to update the current month | |
static void updateMonth(BuildContext context, MonthsOfTheYear newMonth) { | |
final widget = | |
context.dependOnInheritedWidgetOfExactType<CalendarProvider>(); | |
assert(widget != null, 'No CalendarProvider found in context'); | |
widget!.notifier!.value = newMonth; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment