···3434 # The following adds the Cupertino Icons font to your application.
3535 # Use with the CupertinoIcons class for iOS style icons.
3636 cupertino_icons: ^1.0.2
3737+ shared_preferences: ^2.0.15
3838+ get: ^4.6.5
37393840dev_dependencies:
3941 flutter_test:
+64
lib/list_page.dart
···11+import 'package:get/get.dart'
22+ show Get, GetNavigation, GetxController, Inst, Obx, RxInt;
33+import 'package:flutter/material.dart'
44+ show
55+ AlertDialog,
66+ AppBar,
77+ BuildContext,
88+ Center,
99+ ElevatedButton,
1010+ FloatingActionButton,
1111+ Icon,
1212+ Icons,
1313+ Scaffold,
1414+ StatelessWidget,
1515+ Text,
1616+ ThemeData,
1717+ Widget,
1818+ showDialog;
1919+2020+class Controller extends GetxController {
2121+ var count = RxInt(0);
2222+ increment() => count++;
2323+}
2424+2525+class ListPage extends StatelessWidget {
2626+ @override
2727+ Widget build(context) {
2828+ // Instantiate your class using Get.put() to make it available for all "child" routes there.
2929+ final Controller c = Get.put(Controller());
3030+3131+ return Scaffold(
3232+ // Use Obx(()=> to update Text() whenever count is changed.
3333+ appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),
3434+3535+ // Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
3636+ body: Center(
3737+ child: ElevatedButton(
3838+ child: const Text("Go to Other"),
3939+ onPressed: () => Get.to(() => Other()))),
4040+ floatingActionButton: FloatingActionButton(
4141+ child: const Icon(Icons.add),
4242+ onPressed: () {
4343+ var title = Get.isDarkMode ? "Was dark" : "Wasn't dark";
4444+ showDialog(
4545+ context: context,
4646+ builder: (BuildContext context) => AlertDialog(
4747+ title: Text(title),
4848+ ));
4949+ Get.changeTheme(
5050+ Get.isDarkMode ? ThemeData.light() : ThemeData.dark());
5151+ }));
5252+ }
5353+}
5454+5555+class Other extends StatelessWidget {
5656+ // You can ask Get to find a Controller that is being used by another page and redirect you to it.
5757+ final Controller c = Get.find();
5858+5959+ @override
6060+ Widget build(context) {
6161+ // Access the updated count variable
6262+ return Scaffold(body: Center(child: Text("${c.count}")));
6363+ }
6464+}
+32-110
lib/main.dart
···11-import 'package:flutter/material.dart';
11+import 'package:flutter/material.dart'
22+ show ThemeData, ThemeMode, WidgetsFlutterBinding, runApp;
33+import 'theme.dart' show getTheme;
44+import 'list_page.dart' show ListPage;
55+import 'package:shared_preferences/shared_preferences.dart'
66+ show SharedPreferences;
77+import 'package:get/get.dart' show Get, GetMaterialApp, GetNavigation;
2833-void main() {
44- runApp(const MyApp());
99+var prefsFuture = SharedPreferences.getInstance();
1010+1111+void main() async {
1212+ // WidgetsFlutterBinding.ensureInitialized();
1313+ // var prefs = prefsFuture.then((prefs) {
1414+1515+ // })
1616+ // var darkMode = prefs.getBool('darkMode') ?? true;
1717+ runApp(
1818+ GetMaterialApp(
1919+ title: 'Notifier',
2020+ // debugShowCheckedModeBanner: false,
2121+ theme: getTheme(false),
2222+ darkTheme: getTheme(true),
2323+ themeMode: ThemeMode.system,
2424+ home: ListPage()),
2525+ );
2626+ setupDarkMode();
527}
62877-class MyApp extends StatelessWidget {
88- const MyApp({Key? key}) : super(key: key);
99-1010- // This widget is the root of your application.
1111- @override
1212- Widget build(BuildContext context) {
1313- return MaterialApp(
1414- title: 'Flutter Demo',
1515- theme: ThemeData(
1616- // This is the theme of your application.
1717- //
1818- // Try running your application with "flutter run". You'll see the
1919- // application has a blue toolbar. Then, without quitting the app, try
2020- // changing the primarySwatch below to Colors.green and then invoke
2121- // "hot reload" (press "r" in the console where you ran "flutter run",
2222- // or simply save your changes to "hot reload" in a Flutter IDE).
2323- // Notice that the counter didn't reset back to zero; the application
2424- // is not restarted.
2525- primarySwatch: Colors.blue,
2626- ),
2727- home: const MyHomePage(title: 'Flutter Demo Home Page'),
2828- );
2929- }
3030-}
3131-3232-class MyHomePage extends StatefulWidget {
3333- const MyHomePage({Key? key, required this.title}) : super(key: key);
3434-3535- // This widget is the home page of your application. It is stateful, meaning
3636- // that it has a State object (defined below) that contains fields that affect
3737- // how it looks.
3838-3939- // This class is the configuration for the state. It holds the values (in this
4040- // case the title) provided by the parent (in this case the App widget) and
4141- // used by the build method of the State. Fields in a Widget subclass are
4242- // always marked "final".
4343-4444- final String title;
4545-4646- @override
4747- State<MyHomePage> createState() => _MyHomePageState();
4848-}
4949-5050-class _MyHomePageState extends State<MyHomePage> {
5151- int _counter = 0;
5252-5353- void _incrementCounter() {
5454- setState(() {
5555- // This call to setState tells the Flutter framework that something has
5656- // changed in this State, which causes it to rerun the build method below
5757- // so that the display can reflect the updated values. If we changed
5858- // _counter without calling setState(), then the build method would not be
5959- // called again, and so nothing would appear to happen.
6060- _counter++;
6161- });
6262- }
6363-6464- @override
6565- Widget build(BuildContext context) {
6666- // This method is rerun every time setState is called, for instance as done
6767- // by the _incrementCounter method above.
6868- //
6969- // The Flutter framework has been optimized to make rerunning build methods
7070- // fast, so that you can just rebuild anything that needs updating rather
7171- // than having to individually change instances of widgets.
7272- return Scaffold(
7373- appBar: AppBar(
7474- // Here we take the value from the MyHomePage object that was created by
7575- // the App.build method, and use it to set our appbar title.
7676- title: Text(widget.title),
7777- ),
7878- body: Center(
7979- // Center is a layout widget. It takes a single child and positions it
8080- // in the middle of the parent.
8181- child: Column(
8282- // Column is also a layout widget. It takes a list of children and
8383- // arranges them vertically. By default, it sizes itself to fit its
8484- // children horizontally, and tries to be as tall as its parent.
8585- //
8686- // Invoke "debug painting" (press "p" in the console, choose the
8787- // "Toggle Debug Paint" action from the Flutter Inspector in Android
8888- // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
8989- // to see the wireframe for each widget.
9090- //
9191- // Column has various properties to control how it sizes itself and
9292- // how it positions its children. Here we use mainAxisAlignment to
9393- // center the children vertically; the main axis here is the vertical
9494- // axis because Columns are vertical (the cross axis would be
9595- // horizontal).
9696- mainAxisAlignment: MainAxisAlignment.center,
9797- children: <Widget>[
9898- const Text(
9999- 'You have pushed the button this many times:',
100100- ),
101101- Text(
102102- '$_counter',
103103- style: Theme.of(context).textTheme.headline4,
104104- ),
105105- ],
106106- ),
107107- ),
108108- floatingActionButton: FloatingActionButton(
109109- onPressed: _incrementCounter,
110110- tooltip: 'Increment',
111111- child: const Icon(Icons.add),
112112- ), // This trailing comma makes auto-formatting nicer for build methods.
113113- );
2929+void setupDarkMode() async {
3030+ var prefs = await prefsFuture;
3131+ var darkMode = prefs.getBool("darkMode");
3232+ if (darkMode == true) {
3333+ Get.changeTheme(getTheme(true));
3434+ } else {
3535+ Get.changeTheme(getTheme(false));
11436 }
11537}
···11-// This is a basic Flutter widget test.
22-//
33-// To perform an interaction with a widget in your test, use the WidgetTester
44-// utility in the flutter_test package. For example, you can send tap and scroll
55-// gestures. You can also use WidgetTester to find child widgets in the widget
66-// tree, read text, and verify that the values of widget properties are correct.
77-88-import 'package:flutter/material.dart';
99-import 'package:flutter_test/flutter_test.dart';
1010-1111-import 'package:notifier/main.dart';
1212-1313-void main() {
1414- testWidgets('Counter increments smoke test', (WidgetTester tester) async {
1515- // Build our app and trigger a frame.
1616- await tester.pumpWidget(const MyApp());
1717-1818- // Verify that our counter starts at 0.
1919- expect(find.text('0'), findsOneWidget);
2020- expect(find.text('1'), findsNothing);
2121-2222- // Tap the '+' icon and trigger a frame.
2323- await tester.tap(find.byIcon(Icons.add));
2424- await tester.pump();
2525-2626- // Verify that our counter has incremented.
2727- expect(find.text('0'), findsNothing);
2828- expect(find.text('1'), findsOneWidget);
2929- });
3030-}