// // example of changing dark/light theme using hive // ////////////////main.dart//////////////////////// import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; import 'package:hive_flutter/hive_flutter.dart'; import './app_theme.dart'; import './home.dart'; const darkModeBox = 'darkModeTutorial'; void main() async { await Hive.initFlutter(); await Hive.openBox(darkModeBox); runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return ValueListenableBuilder( valueListenable: Hive.box(darkModeBox).listenable(), builder: (context, box, widget) { var darkMode = box.get('darkMode', defaultValue: false); return MaterialApp( theme: AppTheme.lightTheme, darkTheme: AppTheme.darkTheme, themeMode: darkMode ? ThemeMode.dark : ThemeMode.light, home: Home(), ); }, ); } } //////////////////app_theme.dart////////////////////////////// import 'package:flutter/material.dart'; class AppTheme { AppTheme._(); static Color _iconColor = Colors.blueAccent.shade200; static const Color _lightPrimaryColor = Colors.white; static const Color _lightPrimaryVariantColor = Color(0XFFE1E1E1); static const Color _lightSecondaryColor = Colors.green; static const Color _lightOnPrimaryColor = Colors.black; static const Color _darkPrimaryColor = Colors.white24; static const Color _darkPrimaryVariantColor = Colors.black; static const Color _darkSecondaryColor = Colors.white; static const Color _darkOnPrimaryColor = Colors.white; static final ThemeData lightTheme = ThemeData( scaffoldBackgroundColor: _lightPrimaryVariantColor, appBarTheme: AppBarTheme( color: _lightPrimaryVariantColor, iconTheme: IconThemeData(color: _lightOnPrimaryColor), ), colorScheme: ColorScheme.light( primary: _lightPrimaryColor, primaryVariant: _lightPrimaryVariantColor, secondary: _lightSecondaryColor, onPrimary: _lightOnPrimaryColor, ), iconTheme: IconThemeData( color: _iconColor, ), textTheme: _lightTextTheme, ); static final ThemeData darkTheme = ThemeData( scaffoldBackgroundColor: _darkPrimaryVariantColor, appBarTheme: AppBarTheme( color: _darkPrimaryVariantColor, iconTheme: IconThemeData(color: _darkOnPrimaryColor), ), colorScheme: ColorScheme.light( primary: _darkPrimaryColor, primaryVariant: _darkPrimaryVariantColor, secondary: _darkSecondaryColor, onPrimary: _darkOnPrimaryColor, ), iconTheme: IconThemeData( color: _iconColor, ), textTheme: _darkTextTheme, ); static final TextTheme _lightTextTheme = TextTheme( headline: _lightScreenHeadingTextStyle, body1: _lightScreenTaskNameTextStyle, body2: _lightScreenTaskDurationTextStyle, ); static final TextTheme _darkTextTheme = TextTheme( headline: _darkScreenHeadingTextStyle, body1: _darkScreenTaskNameTextStyle, body2: _darkScreenTaskDurationTextStyle, ); static final TextStyle _lightScreenHeadingTextStyle = TextStyle(fontSize: 48.0, color: _lightOnPrimaryColor); static final TextStyle _lightScreenTaskNameTextStyle = TextStyle(fontSize: 20.0, color: _lightOnPrimaryColor); static final TextStyle _lightScreenTaskDurationTextStyle = TextStyle(fontSize: 16.0, color: Colors.grey); static final TextStyle _darkScreenHeadingTextStyle = _lightScreenHeadingTextStyle.copyWith(color: _darkOnPrimaryColor); static final TextStyle _darkScreenTaskNameTextStyle = _lightScreenTaskNameTextStyle.copyWith(color: _darkOnPrimaryColor); static final TextStyle _darkScreenTaskDurationTextStyle = _lightScreenTaskDurationTextStyle; } //////////////////////////home.dart////////////////////////////////////// import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; class Home extends StatelessWidget { var box = Hive.box('darkModeTutorial'); @override Widget build(BuildContext context) { var darkMode = box.get('darkMode', defaultValue: false); return Scaffold( appBar: AppBar( elevation: 0, leading: Padding( padding: const EdgeInsets.all(16), child: Icon(Icons.menu), ), ), body: Column( children: [ Padding( padding: const EdgeInsets.only(left: 16, right: 32, top: 32), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Today", style: Theme.of(context).textTheme.headline, ), Icon(Icons.add_circle_outline), ], ), ), Padding( padding: const EdgeInsets.only(top: 16), child: Card( color: Theme.of(context).colorScheme.primary, elevation: 4, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), margin: const EdgeInsets.symmetric(horizontal: 20), child: ListTile( leading: Icon( Icons.call, color: Theme.of(context).iconTheme.color, ), title: Text( "Conference Call", style: Theme.of(context).textTheme.body1, ), subtitle: Text("30 mins", style: Theme.of(context).textTheme.body2), trailing: Icon(Icons.check_circle, color: Theme.of(context).colorScheme.secondary), ), ), ), Spacer(), Padding( padding: const EdgeInsets.all(16.0), child: Row( children: [ Text('Dark Mode', style: Theme.of(context).textTheme.body2), Spacer(), Switch( value: darkMode, onChanged: (val) { box.put('darkMode', !darkMode); }, ), ], ), ), ], ), ); } }