Skip to content

Commit

Permalink
🐛 fixes #15 Notify listener when changing theme mode
Browse files Browse the repository at this point in the history
  • Loading branch information
BirjuVachhani committed Apr 5, 2021
1 parent cebd319 commit 3cb7e84
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 549 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -74,3 +74,4 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
**/pubspec.lock
278 changes: 0 additions & 278 deletions example/pubspec.lock

This file was deleted.

13 changes: 13 additions & 0 deletions lib/src/adaptive_theme.dart
Expand Up @@ -96,10 +96,12 @@ class _AdaptiveThemeState extends State<AdaptiveTheme>
late ThemeData _defaultTheme;
late ThemeData _defaultDarkTheme;
late _ThemePreferences _preferences;
late ValueNotifier<AdaptiveThemeMode> _modeChangeNotifier;

_AdaptiveThemeState._(
this._defaultTheme, this._defaultDarkTheme, AdaptiveThemeMode mode) {
_theme = _defaultTheme.copyWith();
_modeChangeNotifier = ValueNotifier(mode);
_darkTheme = _defaultDarkTheme.copyWith();
_preferences = _ThemePreferences._initial(mode: mode);
_ThemePreferences._fromPrefs().then((pref) {
Expand All @@ -114,6 +116,9 @@ class _AdaptiveThemeState extends State<AdaptiveTheme>
});
}

@override
ValueNotifier<AdaptiveThemeMode> get modeChangeNotifier => _modeChangeNotifier;

@override
ThemeData get theme => _preferences.mode.isDark ? _darkTheme : _theme;

Expand Down Expand Up @@ -147,6 +152,7 @@ class _AdaptiveThemeState extends State<AdaptiveTheme>
if (mounted) {
setState(() {});
}
_modeChangeNotifier.value = mode;
_preferences._save();
}

Expand Down Expand Up @@ -188,9 +194,16 @@ class _AdaptiveThemeState extends State<AdaptiveTheme>
if (mounted) {
setState(() {});
}
modeChangeNotifier.value = mode;
return _preferences._save();
}

@override
Widget build(BuildContext context) => widget.builder(theme, darkTheme);

@override
void dispose() {
_modeChangeNotifier.dispose();
super.dispose();
}
}
3 changes: 3 additions & 0 deletions lib/src/adaptive_theme_manager.dart
Expand Up @@ -29,6 +29,9 @@ abstract class AdaptiveThemeManager {
/// Returns current theme mode
AdaptiveThemeMode get mode;

/// Allows to listen to changes in them mode.
ValueNotifier<AdaptiveThemeMode> get modeChangeNotifier;

/// checks whether current theme is default theme or not. Default theme
/// refers to he themes provided at the time of initialization
/// of [MaterialApp].
Expand Down

0 comments on commit 3cb7e84

Please sign in to comment.