'Flutter: How can i change a variable based on theme mode (Maps)
im trying to change my flutter-google-maps style based on the theme settings. The dark Map-Style should be displayed when dark mode is activated. This is my code so far:
bool _darkModeEnabled = false;
void _checkIfDarkModeEnabled() {
final ThemeData theme = Theme.of(context);
theme.brightness == Brightness.dark
? _darkModeEnabled = true
: _darkModeEnabled = false;
}
get mapStyle => _darkModeEnabled ? 'assets/map_styles/dark.json' : 'assets/map_styles/light.json';
This is my GoogleMap Code:
body: GoogleMap(
mapType: _currentMapType,
myLocationEnabled: true,
zoomControlsEnabled: false,
compassEnabled: false,
myLocationButtonEnabled: false,
initialCameraPosition: initialLocation,
onMapCreated: (GoogleMapController controller) async {
String style = await DefaultAssetBundle.of(context)
.loadString(mapStyle);
controller.setMapStyle(style);
_controller.complete(controller);
newGoogleMapController = controller;
locatePosition();
},
),
This is my themes.dart:
import 'package:flutter/material.dart';
CustomTheme currentTheme = CustomTheme();
class CustomTheme with ChangeNotifier {
static bool _isDarkTheme = false;
ThemeMode get currentTheme => _isDarkTheme ? ThemeMode.dark : ThemeMode.light;
void toggleTheme() {
_isDarkTheme = !_isDarkTheme;
notifyListeners();
}
static ThemeData get lightTheme {
return ThemeData(
//Color Theme
brightness: Brightness.light,
...
Right now it only changes the map style based on the first line. (bool _darkModeEnabled = false;) if i set it to true the map is dark, if its false the map is light.
Any helped would be appreciated.
Solution 1:[1]
If you want to change the theme of your app at compile time you can modify ThemeData. There is an attribute called Brightness which changes a bunch of colors from light to dark if set to Brightness.dark.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | numanafzal1162 |
