-
-
Save mortezanazari-hub/e98eedd88e2880eac8f01b3a3ca1afd9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const _kThemeMode = 'themeMode'; | |
| /// {@template theme_scope_widget} | |
| /// کلاسی که تمام فرآیندهای تم را مدیریت میکند. | |
| /// | |
| /// متد initialize() باید به عنوان شروع کننده برنامه به منظور استفاده از | |
| /// [AppTheme] در برنامه استفاده شود. | |
| /// A class which handles all theme processes | |
| /// | |
| /// initialize() method should be used as app starter in order to use | |
| /// [AppTheme] in the app | |
| /// {@endtemplate} | |
| class ThemeScopeWidget extends StatefulWidget { | |
| /// {@macro theme_scope_widget} | |
| const ThemeScopeWidget({ | |
| super.key, | |
| required this.child, | |
| required this.preferences, | |
| }); | |
| /// ویجت فرزند. | |
| /// The child widget | |
| final Widget child; | |
| /// تنظیمات اشتراکی. | |
| /// The shared preferences | |
| final SharedPreferences preferences; | |
| /// [ThemeScopeWidget] را با ویجت [child] داده شده مقداردهی اولیه میکند. | |
| /// Initialize the [ThemeScopeWidget] with the given [child] widget | |
| static Future<ThemeScopeWidget> initialize(Widget child) async { | |
| final preferences = await SharedPreferences.getInstance(); | |
| return ThemeScopeWidget( | |
| preferences: preferences, | |
| child: child, | |
| ); | |
| } | |
| /// به منظور استفاده از متدهای [ThemeScopeWidget]، این تابع | |
| /// باید ابتدا فراخوانی شود. فرآیند تغییر تم توسط | |
| /// [ThemeScopeWidget] به صورت خودکار مدیریت خواهد شد. | |
| /// In order to use methods of [ThemeScopeWidget] this function | |
| /// should be called first. Theme change process will handled by | |
| /// [ThemeScopeWidget] automatically. | |
| static ThemeScopeWidgetState? of(BuildContext context) { | |
| return context.findRootAncestorStateOfType<ThemeScopeWidgetState>(); | |
| } | |
| @override | |
| State<ThemeScopeWidget> createState() => ThemeScopeWidgetState(); | |
| } | |
| /// وضعیت برای [ThemeScopeWidget]. | |
| /// The state for [ThemeScopeWidget]. | |
| class ThemeScopeWidgetState extends State<ThemeScopeWidget> { | |
| ThemeMode? _themeMode; | |
| /// حالت تم را تغییر میدهد. | |
| /// Change the theme mode | |
| Future<void> changeTo(ThemeMode themeMode) async { | |
| if (_themeMode == themeMode) return; | |
| try { | |
| final index = ThemeMode.values.indexOf(themeMode); | |
| await widget.preferences.setInt(_kThemeMode, index); | |
| setState(() { | |
| _themeMode = themeMode; | |
| }); | |
| } on Exception catch (_) {} | |
| } | |
| @override | |
| void didChangeDependencies() { | |
| super.didChangeDependencies(); | |
| try { | |
| final themeModeIndex = widget.preferences.getInt(_kThemeMode) ?? 0; | |
| final themeMode = ThemeMode.values[themeModeIndex]; | |
| _themeMode = themeMode; | |
| } on Exception catch (_) { | |
| _themeMode = ThemeMode.system; | |
| } | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| final brightness = MediaQuery.platformBrightnessOf(context); | |
| final appTheme = switch (_themeMode!) { | |
| ThemeMode.light => AppTheme.light(), | |
| ThemeMode.dark => AppTheme.light(), // در اینجا باید تم تاریک قرار بگیرد | |
| ThemeMode.system => | |
| brightness == Brightness.dark ? AppTheme.light() : AppTheme.light(), // در اینجا نیز باید تم تاریک در صورت نیاز قرار بگیرد | |
| }; | |
| return ThemeScope( | |
| themeMode: _themeMode!, | |
| appTheme: appTheme, | |
| child: widget.child, | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment