From 1ba2d49c21dd931a57a8dbe8d85002c979125320 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Sat, 1 Jan 2022 13:43:51 +0100 Subject: [PATCH] refactor: Make style settings null safe --- lib/pages/settings_style/settings_style.dart | 18 ++++--- .../settings_style/settings_style_view.dart | 20 +++---- lib/pages/story/story_view.dart | 54 ++++++++++--------- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/lib/pages/settings_style/settings_style.dart b/lib/pages/settings_style/settings_style.dart index 02db5287..97ac0a20 100644 --- a/lib/pages/settings_style/settings_style.dart +++ b/lib/pages/settings_style/settings_style.dart @@ -1,3 +1,5 @@ +//@dart=2.12 + import 'dart:io'; import 'package:flutter/material.dart'; @@ -12,7 +14,7 @@ import '../../widgets/matrix.dart'; import 'settings_style_view.dart'; class SettingsStyle extends StatefulWidget { - const SettingsStyle({Key key}) : super(key: key); + const SettingsStyle({Key? key}) : super(key: key); @override SettingsStyleController createState() => SettingsStyleController(); @@ -22,18 +24,19 @@ class SettingsStyleController extends State { void setWallpaperAction() async { final wallpaper = await FilePickerCross.importFromStorage(type: FileTypeCross.image); - if (wallpaper == null) return; - Matrix.of(context).wallpaper = File(wallpaper.path); + final path = wallpaper.path; + if (path == null) return; + Matrix.of(context).wallpaper = File(path); await Matrix.of(context) .store .setItem(SettingKeys.wallpaper, wallpaper.path); - setState(() => null); + setState(() {}); } void deleteWallpaperAction() async { Matrix.of(context).wallpaper = null; await Matrix.of(context).store.deleteItem(SettingKeys.wallpaper); - setState(() => null); + setState(() {}); } void setChatColor(Color color) async { @@ -48,7 +51,7 @@ class SettingsStyleController extends State { ); } - AdaptiveThemeMode currentTheme; + AdaptiveThemeMode? currentTheme; static final List customColors = [ AppConfig.primaryColor, @@ -59,7 +62,8 @@ class SettingsStyleController extends State { Colors.blueGrey.shade600, ]; - void switchTheme(AdaptiveThemeMode newTheme) { + void switchTheme(AdaptiveThemeMode? newTheme) { + if (newTheme == null) return; switch (newTheme) { case AdaptiveThemeMode.light: AdaptiveTheme.of(context).setLight(); diff --git a/lib/pages/settings_style/settings_style_view.dart b/lib/pages/settings_style/settings_style_view.dart index 8dd36e65..dd3c36be 100644 --- a/lib/pages/settings_style/settings_style_view.dart +++ b/lib/pages/settings_style/settings_style_view.dart @@ -1,3 +1,5 @@ +//@dart=2.12 + import 'package:flutter/material.dart'; import 'package:adaptive_theme/adaptive_theme.dart'; @@ -11,7 +13,7 @@ import 'settings_style.dart'; class SettingsStyleView extends StatelessWidget { final SettingsStyleController controller; - const SettingsStyleView(this.controller, {Key key}) : super(key: key); + const SettingsStyleView(this.controller, {Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -20,7 +22,7 @@ class SettingsStyleView extends StatelessWidget { return Scaffold( appBar: AppBar( leading: const BackButton(), - title: Text(L10n.of(context).changeTheme), + title: Text(L10n.of(context)!.changeTheme), ), body: MaxWidthBody( withScrolling: true, @@ -59,25 +61,25 @@ class SettingsStyleView extends StatelessWidget { RadioListTile( groupValue: controller.currentTheme, value: AdaptiveThemeMode.system, - title: Text(L10n.of(context).systemTheme), + title: Text(L10n.of(context)!.systemTheme), onChanged: controller.switchTheme, ), RadioListTile( groupValue: controller.currentTheme, value: AdaptiveThemeMode.light, - title: Text(L10n.of(context).lightTheme), + title: Text(L10n.of(context)!.lightTheme), onChanged: controller.switchTheme, ), RadioListTile( groupValue: controller.currentTheme, value: AdaptiveThemeMode.dark, - title: Text(L10n.of(context).darkTheme), + title: Text(L10n.of(context)!.darkTheme), onChanged: controller.switchTheme, ), const Divider(height: 1), ListTile( title: Text( - L10n.of(context).wallpaper, + L10n.of(context)!.wallpaper, style: TextStyle( color: Theme.of(context).colorScheme.secondary, fontWeight: FontWeight.bold, @@ -99,10 +101,10 @@ class SettingsStyleView extends StatelessWidget { ), Builder(builder: (context) { return ListTile( - title: Text(L10n.of(context).changeWallpaper), + title: Text(L10n.of(context)!.changeWallpaper), trailing: Icon( Icons.photo_outlined, - color: Theme.of(context).textTheme.bodyText1.color, + color: Theme.of(context).textTheme.bodyText1?.color, ), onTap: controller.setWallpaperAction, ); @@ -110,7 +112,7 @@ class SettingsStyleView extends StatelessWidget { const Divider(height: 1), ListTile( title: Text( - L10n.of(context).fontSize, + L10n.of(context)!.fontSize, style: TextStyle( color: Theme.of(context).colorScheme.secondary, fontWeight: FontWeight.bold, diff --git a/lib/pages/story/story_view.dart b/lib/pages/story/story_view.dart index e2cf5d8e..9985ca64 100644 --- a/lib/pages/story/story_view.dart +++ b/lib/pages/story/story_view.dart @@ -71,34 +71,36 @@ class StoryView extends StatelessWidget { ), ), ), - actions: [ - if (!controller.isOwnStory && currentEvent != null) - AnimatedOpacity( - duration: const Duration(seconds: 1), - opacity: controller.isHold ? 0 : 1, - child: IconButton( - icon: Icon(Icons.adaptive.share_outlined), - onPressed: controller.share, - ), - ), - AnimatedOpacity( - duration: const Duration(seconds: 1), - opacity: controller.isHold ? 0 : 1, - child: PopupMenuButton( - onSelected: controller.onPopupStoryAction, - itemBuilder: (context) => [ - if (controller.currentEvent?.canRedact ?? false) - PopupMenuItem( - value: PopupStoryAction.delete, - child: Text(L10n.of(context)!.delete), + actions: currentEvent == null + ? null + : [ + if (!controller.isOwnStory) + AnimatedOpacity( + duration: const Duration(seconds: 1), + opacity: controller.isHold ? 0 : 1, + child: IconButton( + icon: Icon(Icons.adaptive.share_outlined), + onPressed: controller.share, ), - PopupMenuItem( - value: PopupStoryAction.report, - child: Text(L10n.of(context)!.reportMessage), ), - ], - )), - ], + AnimatedOpacity( + duration: const Duration(seconds: 1), + opacity: controller.isHold ? 0 : 1, + child: PopupMenuButton( + onSelected: controller.onPopupStoryAction, + itemBuilder: (context) => [ + if (controller.currentEvent?.canRedact ?? false) + PopupMenuItem( + value: PopupStoryAction.delete, + child: Text(L10n.of(context)!.delete), + ), + PopupMenuItem( + value: PopupStoryAction.report, + child: Text(L10n.of(context)!.reportMessage), + ), + ], + )), + ], systemOverlayStyle: SystemUiOverlayStyle.light, iconTheme: const IconThemeData(color: Colors.white), elevation: 0,