theme_notifier.dart 761 B

123456789101112131415161718192021222324252627
  1. import 'package:flutter/material.dart';
  2. import 'package:riverpod/riverpod.dart';
  3. final themeProvider = StateNotifierProvider<ThemeNotifier, ThemeMode>((ref) {
  4. return ThemeNotifier();
  5. });
  6. class ThemeNotifier extends StateNotifier<ThemeMode> {
  7. //这里应该根据用户的SP配置来设置不同的主题,先偷个懒这里我先写死跟随系统的主题
  8. ThemeNotifier() : super(ThemeMode.system); //默认 system 主题
  9. //手动切换主题
  10. void toggleTheme() {
  11. if (state == ThemeMode.light) {
  12. state = ThemeMode.dark;
  13. } else if (state == ThemeMode.dark) {
  14. state = ThemeMode.light;
  15. } else {
  16. state = ThemeMode.light;
  17. }
  18. }
  19. //跟随系统主题
  20. void followSystemTheme() {
  21. state = ThemeMode.system;
  22. }
  23. }