如何让Get.isDarkMode改变图标?

问题描述 投票:0回答:1

我可以在我的应用程序中更改主题,但我在使用

Get.isDarkMode
时遇到问题,当我尝试更改
text color
或我尝试更改
icon

时,它不听。

import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';

class ThemeService {
  final _box = GetStorage();
  final _key = 'isDarkMode';

  ThemeMode get theme => _loadThemeFromBox() ? ThemeMode.dark : ThemeMode.light;

  bool _loadThemeFromBox() => _box.read(_key) ?? false;

  _saveThemeToBox(bool isDarkMode) => _box.write(_key, isDarkMode);

  void switchTheme() {
    Get.changeThemeMode(_loadThemeFromBox() ? ThemeMode.light : ThemeMode.dark);
    _saveThemeToBox(!_loadThemeFromBox());
  }
}
 IconButton( icon: Icon(Get.isDarkMode ? Icons.dark_mode_outlined : Icons.light_mode_outlined) , onPressed: (){
            ThemeService().switchTheme();
          },)
flutter dart flutter-layout flutter-dependencies
1个回答
0
投票

在使用 Get.isDarkMode 之前使用 context.isDarkMode

context.isDarkMode

例如enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.