DsThemeController class

Persists ThemeMode and DsBrand across app restarts using SharedPreferences.

───────────────────────────────────────────── Quick start ─────────────────────────────────────────────

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final ctrl = DsThemeController();
  await ctrl.init();
  runApp(MyApp(controller: ctrl));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key, required this.controller});
  final DsThemeController controller;

  @override
  Widget build(BuildContext context) {
    return ListenableBuilder(
      listenable: controller,
      builder: (_, __) => MaterialApp(
        theme: controller.lightTheme,
        darkTheme: controller.darkTheme,
        themeMode: controller.themeMode,
        home: Scaffold(
          appBar: AppBar(
            actions: [
              DsBrandToggle(
                brand: controller.brand,
                onChanged: controller.setBrand,
              ),
              DsThemeToggle(
                themeMode: controller.themeMode,
                onChanged: controller.setThemeMode,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Inheritance

Constructors

DsThemeController({ThemeMode defaultThemeMode = ThemeMode.system, DsBrand defaultBrand = DsBrand.violet, Locale defaultLocale = const Locale('en')})

Properties

brand DsBrand
The currently active DsBrand.
no setter
darkTheme ThemeData
Convenience getter — the dark ThemeData for the active brand.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
lightTheme ThemeData
Convenience getter — the light ThemeData for the active brand.
no setter
locale Locale
The currently active Locale.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
themeMode ThemeMode
The currently active ThemeMode.
no setter

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
inherited
init() Future<void>
Loads persisted values from SharedPreferences.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
setBrand(DsBrand brand) → void
Updates the DsBrand and persists the choice.
setLocale(Locale locale) → void
Updates the Locale and persists the choice.
setThemeMode(ThemeMode mode) → void
Updates the ThemeMode and persists the choice.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited