Theme Extension
These helpers use package:material_ui/material_ui.dart theme types. Place your context below a material_ui.MaterialApp or matching Theme; SDK Material theme types are different.
themeData
Quickly get the current ThemeData from the nearest Theme widget ancestor in the widget tree.
final theme = context.themeData;textTheme
Effortlessly obtain the TextTheme for your context.
final textTheme = context.textTheme;brightness
Determine the brightness setting of the system and the app theme. This is useful for toggling between light and dark modes.
final themeBrightness = context.brightness;
final systemBrightness = context.sysBrightness;sysBrightness
Determine the brightness setting of the system and the app theme. This is useful for toggling between light and dark modes.
final themeBrightness = context.brightness;
final systemBrightness = context.sysBrightness;isDark and isLight
Convenient booleans to quickly check if the current theme is dark or light.
final isDarkMode = context.isDark;
final isLightMode = context.isLight;Example Usage
Here is an example that shows how to use these extensions to dynamically change the text color based on the current theme's brightness.
class ThemedText extends StatelessWidget {
final String text;
ThemedText(this.text);
@override
Widget build(BuildContext context) {
// Access text theme and brightness
final textTheme = context.textTheme;
final isDarkMode = context.isDark;
return Text(
text,
style: textTheme.bodyLarge?.copyWith(
color: isDarkMode ? Colors.white : Colors.black,
),
);
}
}Utilizing ThemeExtension from flutter_helper_utils will make your code cleaner and more maintainable while working with themes.