From 95b46b0d8514a831e3ca805095eef3b031d09815 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 20 Sep 2021 21:52:15 +0200 Subject: [PATCH] adaptive theme using adaptive_theme plugin --- lib/main.dart | 43 ++++++++++++++++++------------ lib/screens/home/home.dart | 6 ++--- lib/screens/settings/settings.dart | 22 ++++++++++++--- pubspec.lock | 7 +++++ pubspec.yaml | 1 + test/widget_test.dart | 30 --------------------- 6 files changed, 56 insertions(+), 53 deletions(-) delete mode 100644 test/widget_test.dart diff --git a/lib/main.dart b/lib/main.dart index 34570fe..b1ecd42 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,31 +1,40 @@ import 'package:flutter/material.dart'; import 'package:notes_app/screens/home/home.dart'; import 'package:notes_app/components/storage.dart'; +import 'package:adaptive_theme/adaptive_theme.dart'; -void main() { - runApp(MyApp()); +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + final savedThemeMode = await AdaptiveTheme.getThemeMode(); + runApp(MyApp(savedThemeMode: savedThemeMode)); } class MyApp extends StatelessWidget { - + const MyApp({ Key? key, required this.savedThemeMode }) : super(key: key); + + final savedThemeMode; + // This widget is the root of your application. @override Widget build(BuildContext context) { - return MaterialApp( - title: 'Notes App', - theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. - primarySwatch: Colors.blue, + return AdaptiveTheme( + light: ThemeData( + brightness: Brightness.light, + primaryColor: Colors.white, + accentColor: Colors.black, + ), + dark: ThemeData( + brightness: Brightness.dark, + primarySwatch: Colors.red, + accentColor: Colors.amber, + ), + initial: savedThemeMode ?? AdaptiveThemeMode.system, + builder: (theme, darkTheme) => MaterialApp( + title: 'Simple Notes', + theme: theme, + darkTheme: darkTheme, + home: MyHomePage(storage: CounterStorage(),), ), - home: MyHomePage(storage: CounterStorage()), ); } } \ No newline at end of file diff --git a/lib/screens/home/home.dart b/lib/screens/home/home.dart index 312b955..3402e82 100644 --- a/lib/screens/home/home.dart +++ b/lib/screens/home/home.dart @@ -79,7 +79,7 @@ class _MyHomePageState extends State { widget.storage.openNote(context, ""); }, tooltip: 'Take a new note', - child: Icon(Icons.note_alt_sharp), + child: Icon(Icons.add), ), ), onWillPop: () async { @@ -168,7 +168,7 @@ class _MyHomePageState extends State { /* DROP DOWN MENUS*/ DropdownButton btnAppBarMoreSelect() { return DropdownButton( - icon: const Icon(Icons.more_vert, color: Colors.white), + icon: const Icon(Icons.more_vert), onChanged: (String? newValue) { switch (newValue) { case "Delete": @@ -224,7 +224,7 @@ class _MyHomePageState extends State { DropdownButton btnAppBarMoreNoSelect(BuildContext context) { return DropdownButton( - icon: const Icon(Icons.more_vert, color: Colors.white), + icon: const Icon(Icons.more_vert), onChanged: (String? newValue) { switch (newValue) { case "Select": diff --git a/lib/screens/settings/settings.dart b/lib/screens/settings/settings.dart index f47907b..0cdb755 100644 --- a/lib/screens/settings/settings.dart +++ b/lib/screens/settings/settings.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:notes_app/components/storage.dart'; import 'package:notes_app/screens/home/home.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:adaptive_theme/adaptive_theme.dart'; class Settings extends StatefulWidget { const Settings({Key? key, required this.storage}) : super(key: key); @@ -135,11 +136,26 @@ class _SettingsState extends State { }); } - void _setTheme(dynamic newTheme) async { - final prefs = await SharedPreferences.getInstance(); - prefs.setString("theme", _theme); + void _setTheme(dynamic newTheme) { + // final prefs = await SharedPreferences.getInstance(); + + // prefs.setString("theme", _theme); setState(() { _theme = newTheme; + switch (_theme) { + case "Dark": + // sets theme mode to dark + AdaptiveTheme.of(context).setDark(); + break; + case "Light": + // sets theme mode to light + AdaptiveTheme.of(context).setLight(); + break; + case "Follow System": + // sets theme mode to system default + AdaptiveTheme.of(context).setSystem(); + break; + } Navigator.of(context).pop(); }); } diff --git a/pubspec.lock b/pubspec.lock index b777e46..dc7de1f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + adaptive_theme: + dependency: "direct main" + description: + name: adaptive_theme + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0" async: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 2d57bdc..fe8565c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: cupertino_icons: ^1.0.3 path_provider: ^2.0.4 shared_preferences: ^2.0.7 + adaptive_theme: ^2.2.0 dev_dependencies: flutter_test: diff --git a/test/widget_test.dart b/test/widget_test.dart deleted file mode 100644 index c80a0ca..0000000 --- a/test/widget_test.dart +++ /dev/null @@ -1,30 +0,0 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility that Flutter provides. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'package:notes_app/main.dart'; - -void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(MyApp()); - - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); - - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); - - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); - }); -}