From 8266025af95a138518560078f5d550185d5868c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Tartanus?= Date: Thu, 18 Mar 2021 00:12:53 +0100 Subject: [PATCH] Migration to null safety and Flutter v2 - fix #51 --- example/.gitignore | 2 + example/lib/main.dart | 10 +- example/pubspec.lock | 175 +++++++++++++++++++++++++++---- lib/dynamic_theme.dart | 22 ++-- lib/theme_switcher_widgets.dart | 6 +- pubspec.lock | 173 ++++++++++++++++++++++++++---- pubspec.yaml | 42 +------- test/easy_theme_test.dart | 5 +- test/easy_theme_test_change.dart | 17 +-- 9 files changed, 340 insertions(+), 112 deletions(-) diff --git a/example/.gitignore b/example/.gitignore index c9edd23..9e28883 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -7,5 +7,7 @@ build/ .flutter-plugins +.flutter-plugins-dependencies +ios/Flutter/flutter_export_environment.sh .idea/* \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 7ddf402..1ea5fa3 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -44,11 +44,11 @@ class _MyHomePageState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - RaisedButton( + ElevatedButton( onPressed: DynamicTheme.of(context).toggleBrightness, child: const Text('Toggle brightness'), ), - RaisedButton( + ElevatedButton( onPressed: changeColor, child: const Text('Change color'), ), @@ -60,14 +60,14 @@ class _MyHomePageState extends State { child: const Icon(Icons.add), ), bottomNavigationBar: BottomNavigationBar( - items: [ + items: const [ BottomNavigationBarItem( icon: Icon(Icons.insert_drive_file), - title: const Text('Tab 1'), + label: 'Tab 1', ), BottomNavigationBarItem( icon: Icon(Icons.show_chart), - title: const Text('Tab 2'), + label: 'Tab 2', ), ], ), diff --git a/example/pubspec.lock b/example/pubspec.lock index a73b69d..3428756 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: async: dependency: transitive @@ -7,28 +7,42 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.2.0" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: @@ -42,7 +56,28 @@ packages: path: ".." relative: true source: path - version: "1.0.1" + version: "2.0.0-nullsafety" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.0" flutter: dependency: "direct main" description: flutter @@ -53,41 +88,123 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.3" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.3+1" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" - quiver: + version: "1.8.0" + path_provider_linux: dependency: transitive description: - name: quiver + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.1.0" shared_preferences: dependency: transitive description: name: shared_preferences url: "https://pub.dartlang.org" source: hosted - version: "0.5.0" + version: "2.0.5" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" sky_engine: dependency: transitive description: flutter @@ -99,56 +216,70 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.4.1" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "1.6.8" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.1" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" sdks: - dart: ">=2.0.0 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + dart: ">=2.12.0 <3.0.0" + flutter: ">=1.20.0" diff --git a/lib/dynamic_theme.dart b/lib/dynamic_theme.dart index 2aeba4a..8f6d988 100644 --- a/lib/dynamic_theme.dart +++ b/lib/dynamic_theme.dart @@ -3,17 +3,15 @@ import 'dart:async'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter/material.dart'; -typedef ThemedWidgetBuilder = Widget Function( - BuildContext context, ThemeData data); +typedef ThemedWidgetBuilder = Widget Function(BuildContext context, ThemeData data); -typedef ThemeDataWithBrightnessBuilder = ThemeData Function( - Brightness brightness); +typedef ThemeDataWithBrightnessBuilder = ThemeData Function(Brightness brightness); class DynamicTheme extends StatefulWidget { const DynamicTheme({ - Key key, - this.data, - this.themedWidgetBuilder, + Key? key, + required this.data, + required this.themedWidgetBuilder, this.defaultBrightness = Brightness.light, this.loadBrightnessOnStart = true, }) : super(key: key); @@ -37,17 +35,17 @@ class DynamicTheme extends StatefulWidget { @override DynamicThemeState createState() => DynamicThemeState(); - static DynamicThemeState of(BuildContext context) { - return context.findAncestorStateOfType>(); + static DynamicThemeState? of(BuildContext context) { + return context.findAncestorStateOfType(); } } class DynamicThemeState extends State { - ThemeData _themeData; + late ThemeData _themeData; - Brightness _brightness; + Brightness _brightness = Brightness.light; - bool _shouldLoadBrightness; + bool _shouldLoadBrightness = true; static const String _sharedPreferencesKey = 'isDark'; diff --git a/lib/theme_switcher_widgets.dart b/lib/theme_switcher_widgets.dart index a396be5..bc1ecc6 100644 --- a/lib/theme_switcher_widgets.dart +++ b/lib/theme_switcher_widgets.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; class BrightnessSwitcherDialog extends StatelessWidget { - const BrightnessSwitcherDialog({Key key, this.onSelectedTheme}) + const BrightnessSwitcherDialog({Key? key, required this.onSelectedTheme}) : super(key: key); final ValueChanged onSelectedTheme; @@ -14,7 +14,7 @@ class BrightnessSwitcherDialog extends StatelessWidget { RadioListTile( value: Brightness.light, groupValue: Theme.of(context).brightness, - onChanged: (Brightness value) { + onChanged: (Brightness? value) { onSelectedTheme(Brightness.light); }, title: const Text('Light'), @@ -22,7 +22,7 @@ class BrightnessSwitcherDialog extends StatelessWidget { RadioListTile( value: Brightness.dark, groupValue: Theme.of(context).brightness, - onChanged: (Brightness value) { + onChanged: (Brightness? value) { onSelectedTheme(Brightness.dark); }, title: const Text('Spooky 👻'), diff --git a/pubspec.lock b/pubspec.lock index 8811910..4417315 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: async: dependency: transitive @@ -7,28 +7,63 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.2.0" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.15.0" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.0" flutter: dependency: "direct main" description: flutter @@ -39,41 +74,123 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.3" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.3+1" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" - quiver: + version: "1.8.0" + path_provider_linux: dependency: transitive description: - name: quiver + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.1.0" shared_preferences: dependency: "direct main" description: name: shared_preferences url: "https://pub.dartlang.org" source: hosted - version: "0.5.0" + version: "2.0.5" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" sky_engine: dependency: transitive description: flutter @@ -85,56 +202,70 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.4.1" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "1.6.8" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.1" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" sdks: - dart: ">=2.0.0 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + dart: ">=2.12.0 <3.0.0" + flutter: ">=1.20.0" diff --git a/pubspec.yaml b/pubspec.yaml index 3ac4037..e23a839 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,53 +1,17 @@ name: dynamic_theme description: Changes the theme during runtime, also presists brightness settings across restarts -version: 1.0.1 +version: 2.0.0-nullsafety author: 'Norbert Kozsir ' homepage: 'https://github.com/Norbert515/dynamic_theme' environment: - # Works in Dart 1 (starting with 1.20.1), and works in Dart 2. - sdk: '>=1.20.1 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter - shared_preferences: '^0.5.0' + shared_preferences: ^2.0.5 dev_dependencies: flutter_test: sdk: flutter - -# For information on the generic Dart part of this file, see the -# following page: https://www.dartlang.org/tools/pub/pubspec - -# The following section is specific to Flutter. -flutter: - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.io/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.io/assets-and-images/#resolution-aware. - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.io/custom-fonts/#from-packages diff --git a/test/easy_theme_test.dart b/test/easy_theme_test.dart index 4d564fa..25c4809 100644 --- a/test/easy_theme_test.dart +++ b/test/easy_theme_test.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; ValueKey key = const ValueKey('ok'); -DynamicThemeState state; +DynamicThemeState? state; GlobalKey easyThemeKey = GlobalKey(); void main() { @@ -44,11 +44,12 @@ class MyApp extends StatelessWidget { class ButtonPage extends StatelessWidget { @override Widget build(BuildContext context) { - return RaisedButton( + return ElevatedButton( onPressed: () { state = DynamicTheme.of(context); }, key: key, + child: Container(), ); } } diff --git a/test/easy_theme_test_change.dart b/test/easy_theme_test_change.dart index 2ab623a..952d2be 100644 --- a/test/easy_theme_test_change.dart +++ b/test/easy_theme_test_change.dart @@ -10,20 +10,20 @@ void main() { testWidgets('change brightness', (WidgetTester tester) async { await tester.pumpWidget(MyApp()); - MaterialApp app = find.byType(MaterialApp).evaluate().first.widget; - expect(app.theme.brightness, equals(Brightness.dark)); + MaterialApp app = find.byType(MaterialApp).evaluate().first.widget as MaterialApp; + expect(app.theme?.brightness, equals(Brightness.dark)); await tester.tap(find.byKey(key)); await tester.pumpAndSettle(); - app = find.byType(MaterialApp).evaluate().first.widget; - expect(app.theme.brightness, equals(Brightness.light)); + app = find.byType(MaterialApp).evaluate().first.widget as MaterialApp; + expect(app.theme?.brightness, equals(Brightness.light)); await tester.tap(find.byKey(key)); await tester.pumpAndSettle(); - app = find.byType(MaterialApp).evaluate().first.widget; - expect(app.theme.brightness, equals(Brightness.dark)); + app = find.byType(MaterialApp).evaluate().first.widget as MaterialApp; + expect(app.theme?.brightness, equals(Brightness.dark)); }); } @@ -52,14 +52,15 @@ class MyApp extends StatelessWidget { class ButtonPage extends StatelessWidget { @override Widget build(BuildContext context) { - return RaisedButton( + return ElevatedButton( onPressed: () { - DynamicTheme.of(context).setBrightness( + DynamicTheme.of(context)?.setBrightness( Theme.of(context).brightness == Brightness.dark ? Brightness.light : Brightness.dark); }, key: key, + child: Container(), ); } }