adaptive theme using adaptive_theme plugin

master
EmaMaker 2021-09-20 21:52:15 +02:00
parent b96e227b57
commit 95b46b0d85
6 changed files with 56 additions and 53 deletions

View File

@ -1,31 +1,40 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:notes_app/screens/home/home.dart'; import 'package:notes_app/screens/home/home.dart';
import 'package:notes_app/components/storage.dart'; import 'package:notes_app/components/storage.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
void main() { void main() async {
runApp(MyApp()); WidgetsFlutterBinding.ensureInitialized();
final savedThemeMode = await AdaptiveTheme.getThemeMode();
runApp(MyApp(savedThemeMode: savedThemeMode));
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({ Key? key, required this.savedThemeMode }) : super(key: key);
final savedThemeMode;
// This widget is the root of your application. // This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return AdaptiveTheme(
title: 'Notes App', light: ThemeData(
theme: ThemeData( brightness: Brightness.light,
// This is the theme of your application. primaryColor: Colors.white,
// accentColor: Colors.black,
// Try running your application with "flutter run". You'll see the ),
// application has a blue toolbar. Then, without quitting the app, try dark: ThemeData(
// changing the primarySwatch below to Colors.green and then invoke brightness: Brightness.dark,
// "hot reload" (press "r" in the console where you ran "flutter run", primarySwatch: Colors.red,
// or simply save your changes to "hot reload" in a Flutter IDE). accentColor: Colors.amber,
// Notice that the counter didn't reset back to zero; the application ),
// is not restarted. initial: savedThemeMode ?? AdaptiveThemeMode.system,
primarySwatch: Colors.blue, builder: (theme, darkTheme) => MaterialApp(
title: 'Simple Notes',
theme: theme,
darkTheme: darkTheme,
home: MyHomePage(storage: CounterStorage(),),
), ),
home: MyHomePage(storage: CounterStorage()),
); );
} }
} }

View File

@ -79,7 +79,7 @@ class _MyHomePageState extends State<MyHomePage> {
widget.storage.openNote(context, ""); widget.storage.openNote(context, "");
}, },
tooltip: 'Take a new note', tooltip: 'Take a new note',
child: Icon(Icons.note_alt_sharp), child: Icon(Icons.add),
), ),
), ),
onWillPop: () async { onWillPop: () async {
@ -168,7 +168,7 @@ class _MyHomePageState extends State<MyHomePage> {
/* DROP DOWN MENUS*/ /* DROP DOWN MENUS*/
DropdownButton btnAppBarMoreSelect() { DropdownButton btnAppBarMoreSelect() {
return DropdownButton<String>( return DropdownButton<String>(
icon: const Icon(Icons.more_vert, color: Colors.white), icon: const Icon(Icons.more_vert),
onChanged: (String? newValue) { onChanged: (String? newValue) {
switch (newValue) { switch (newValue) {
case "Delete": case "Delete":
@ -224,7 +224,7 @@ class _MyHomePageState extends State<MyHomePage> {
DropdownButton btnAppBarMoreNoSelect(BuildContext context) { DropdownButton btnAppBarMoreNoSelect(BuildContext context) {
return DropdownButton<String>( return DropdownButton<String>(
icon: const Icon(Icons.more_vert, color: Colors.white), icon: const Icon(Icons.more_vert),
onChanged: (String? newValue) { onChanged: (String? newValue) {
switch (newValue) { switch (newValue) {
case "Select": case "Select":

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:notes_app/components/storage.dart'; import 'package:notes_app/components/storage.dart';
import 'package:notes_app/screens/home/home.dart'; import 'package:notes_app/screens/home/home.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
class Settings extends StatefulWidget { class Settings extends StatefulWidget {
const Settings({Key? key, required this.storage}) : super(key: key); const Settings({Key? key, required this.storage}) : super(key: key);
@ -135,11 +136,26 @@ class _SettingsState extends State<Settings> {
}); });
} }
void _setTheme(dynamic newTheme) async { void _setTheme(dynamic newTheme) {
final prefs = await SharedPreferences.getInstance(); // final prefs = await SharedPreferences.getInstance();
prefs.setString("theme", _theme);
// prefs.setString("theme", _theme);
setState(() { setState(() {
_theme = newTheme; _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(); Navigator.of(context).pop();
}); });
} }

View File

@ -1,6 +1,13 @@
# Generated by pub # Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
adaptive_theme:
dependency: "direct main"
description:
name: adaptive_theme
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
async: async:
dependency: transitive dependency: transitive
description: description:

View File

@ -30,6 +30,7 @@ dependencies:
cupertino_icons: ^1.0.3 cupertino_icons: ^1.0.3
path_provider: ^2.0.4 path_provider: ^2.0.4
shared_preferences: ^2.0.7 shared_preferences: ^2.0.7
adaptive_theme: ^2.2.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@ -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);
});
}