gt_theme 1.0.0
gt_theme: ^1.0.0 copied to clipboard
A production-ready Flutter theme management package with Material 3 & Cupertino support, system theme detection, light/dark mode switching, and theme persistence via SharedPreferences.
GT Theme #
A production-ready Flutter theme management package with Material 3 & Cupertino support, system theme detection, light/dark mode switching, and theme persistence.
β¨ Features #
- π¨ Material 3 & Cupertino Support - Beautiful pre-configured themes for both design systems
- π Light/Dark/System Modes - Complete theme mode switching with persistence
- π± System Theme Detection - Real-time sync with device theme changes (no context needed!)
- πΎ Theme Persistence - Automatically saves user preference via SharedPreferences
- β‘ High Performance - Minimal rebuilds using ValueNotifier pattern
- π Zero-Dependency State Management - Works with GetX, Provider, Bloc, Riverpod, or none!
- π§© Ready-to-Use Widgets - ThemeModeSelector, BrightnessIndicator, ThemeInfoCard
π¦ Installation #
Add to your pubspec.yaml:
dependencies:
gt_theme: ^1.0.0
Then run:
flutter pub get
π Quick Start #
1. Initialize in main() #
import 'package:flutter/material.dart';
import 'package:gt_theme/gt_theme.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
ThemeService.instance.startListening();
runApp(const MyApp());
}
2. Configure MaterialApp #
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<ThemeMode>(
valueListenable: ThemeService.instance.themeModeNotifier,
builder: (context, themeMode, child) {
return MaterialApp(
title: 'My App',
themeMode: themeMode,
theme: ThemeService.instance.getMaterialTheme(Brightness.light),
darkTheme: ThemeService.instance.getMaterialTheme(Brightness.dark),
home: const HomePage(),
);
},
);
}
}
3. Use Theme Widgets #
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
// Theme mode selector (Light/Dark/System)
ThemeModeSelector(
themeService: ThemeService.instance,
onThemeModeChanged: () => print('Theme changed!'),
),
// Shows current system brightness
BrightnessIndicator(themeService: ThemeService.instance),
// Shows detailed theme info
ThemeInfoCard(themeService: ThemeService.instance),
],
),
);
}
}
π API Reference #
ThemeService #
The core singleton service for theme management.
| Property/Method | Description |
|---|---|
ThemeService.instance |
Global singleton instance |
startListening() |
Start listening to system brightness changes |
setThemeMode(ThemeMode) |
Set theme mode (light/dark/system) |
currentThemeMode |
Get current ThemeMode |
effectiveBrightness |
Get effective Brightness based on mode |
themeModeNotifier |
ValueNotifier for theme mode changes |
systemBrightnessNotifier |
ValueNotifier for system brightness |
getMaterialTheme(Brightness) |
Get Material ThemeData |
getCupertinoTheme(Brightness) |
Get CupertinoThemeData |
Widgets #
| Widget | Description |
|---|---|
ThemeConsumer |
Efficiently consume theme data with minimal rebuilds |
ThemeModeSelector |
UI for switching between Light/Dark/System modes |
BrightnessIndicator |
Display current system brightness |
ThemeInfoCard |
Display current theme information |
π― Advanced Usage #
Manual Theme Switching #
// Switch to dark mode
ThemeService.instance.setThemeMode(ThemeMode.dark);
// Switch to light mode
ThemeService.instance.setThemeMode(ThemeMode.light);
// Follow system theme
ThemeService.instance.setThemeMode(ThemeMode.system);
Using ThemeConsumer for Optimized Rebuilds #
ThemeConsumer(
themeService: ThemeService.instance,
builder: (context, service, child) {
final isDark = service.effectiveBrightness == Brightness.dark;
return Container(
color: isDark ? Colors.black : Colors.white,
child: child, // This child won't rebuild!
);
},
child: const ExpensiveWidget(), // Won't rebuild on theme change
)
Cupertino App Support #
CupertinoApp(
theme: ThemeService.instance.getCupertinoTheme(
ThemeService.instance.effectiveBrightness,
),
home: const HomePage(),
)
π€ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
π License #
This project is licensed under the MIT License - see the LICENSE file for details.
π¨βπ» Author #
Gaurav Technical
- GitHub: @GauravTechnical24