adaptive theme using adaptive_theme plugin
parent
b96e227b57
commit
95b46b0d85
|
@ -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()),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -79,7 +79,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
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<MyHomePage> {
|
|||
/* DROP DOWN MENUS*/
|
||||
DropdownButton btnAppBarMoreSelect() {
|
||||
return DropdownButton<String>(
|
||||
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<MyHomePage> {
|
|||
|
||||
DropdownButton btnAppBarMoreNoSelect(BuildContext context) {
|
||||
return DropdownButton<String>(
|
||||
icon: const Icon(Icons.more_vert, color: Colors.white),
|
||||
icon: const Icon(Icons.more_vert),
|
||||
onChanged: (String? newValue) {
|
||||
switch (newValue) {
|
||||
case "Select":
|
||||
|
|
|
@ -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<Settings> {
|
|||
});
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue