refactor: Make style settings null safe

This commit is contained in:
Krille Fear 2022-01-01 13:43:51 +01:00
parent 03aabec300
commit 1ba2d49c21
3 changed files with 50 additions and 42 deletions

View File

@ -1,3 +1,5 @@
//@dart=2.12
import 'dart:io'; import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -12,7 +14,7 @@ import '../../widgets/matrix.dart';
import 'settings_style_view.dart'; import 'settings_style_view.dart';
class SettingsStyle extends StatefulWidget { class SettingsStyle extends StatefulWidget {
const SettingsStyle({Key key}) : super(key: key); const SettingsStyle({Key? key}) : super(key: key);
@override @override
SettingsStyleController createState() => SettingsStyleController(); SettingsStyleController createState() => SettingsStyleController();
@ -22,18 +24,19 @@ class SettingsStyleController extends State<SettingsStyle> {
void setWallpaperAction() async { void setWallpaperAction() async {
final wallpaper = final wallpaper =
await FilePickerCross.importFromStorage(type: FileTypeCross.image); await FilePickerCross.importFromStorage(type: FileTypeCross.image);
if (wallpaper == null) return; final path = wallpaper.path;
Matrix.of(context).wallpaper = File(wallpaper.path); if (path == null) return;
Matrix.of(context).wallpaper = File(path);
await Matrix.of(context) await Matrix.of(context)
.store .store
.setItem(SettingKeys.wallpaper, wallpaper.path); .setItem(SettingKeys.wallpaper, wallpaper.path);
setState(() => null); setState(() {});
} }
void deleteWallpaperAction() async { void deleteWallpaperAction() async {
Matrix.of(context).wallpaper = null; Matrix.of(context).wallpaper = null;
await Matrix.of(context).store.deleteItem(SettingKeys.wallpaper); await Matrix.of(context).store.deleteItem(SettingKeys.wallpaper);
setState(() => null); setState(() {});
} }
void setChatColor(Color color) async { void setChatColor(Color color) async {
@ -48,7 +51,7 @@ class SettingsStyleController extends State<SettingsStyle> {
); );
} }
AdaptiveThemeMode currentTheme; AdaptiveThemeMode? currentTheme;
static final List<Color> customColors = [ static final List<Color> customColors = [
AppConfig.primaryColor, AppConfig.primaryColor,
@ -59,7 +62,8 @@ class SettingsStyleController extends State<SettingsStyle> {
Colors.blueGrey.shade600, Colors.blueGrey.shade600,
]; ];
void switchTheme(AdaptiveThemeMode newTheme) { void switchTheme(AdaptiveThemeMode? newTheme) {
if (newTheme == null) return;
switch (newTheme) { switch (newTheme) {
case AdaptiveThemeMode.light: case AdaptiveThemeMode.light:
AdaptiveTheme.of(context).setLight(); AdaptiveTheme.of(context).setLight();

View File

@ -1,3 +1,5 @@
//@dart=2.12
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:adaptive_theme/adaptive_theme.dart'; import 'package:adaptive_theme/adaptive_theme.dart';
@ -11,7 +13,7 @@ import 'settings_style.dart';
class SettingsStyleView extends StatelessWidget { class SettingsStyleView extends StatelessWidget {
final SettingsStyleController controller; final SettingsStyleController controller;
const SettingsStyleView(this.controller, {Key key}) : super(key: key); const SettingsStyleView(this.controller, {Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -20,7 +22,7 @@ class SettingsStyleView extends StatelessWidget {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
leading: const BackButton(), leading: const BackButton(),
title: Text(L10n.of(context).changeTheme), title: Text(L10n.of(context)!.changeTheme),
), ),
body: MaxWidthBody( body: MaxWidthBody(
withScrolling: true, withScrolling: true,
@ -59,25 +61,25 @@ class SettingsStyleView extends StatelessWidget {
RadioListTile<AdaptiveThemeMode>( RadioListTile<AdaptiveThemeMode>(
groupValue: controller.currentTheme, groupValue: controller.currentTheme,
value: AdaptiveThemeMode.system, value: AdaptiveThemeMode.system,
title: Text(L10n.of(context).systemTheme), title: Text(L10n.of(context)!.systemTheme),
onChanged: controller.switchTheme, onChanged: controller.switchTheme,
), ),
RadioListTile<AdaptiveThemeMode>( RadioListTile<AdaptiveThemeMode>(
groupValue: controller.currentTheme, groupValue: controller.currentTheme,
value: AdaptiveThemeMode.light, value: AdaptiveThemeMode.light,
title: Text(L10n.of(context).lightTheme), title: Text(L10n.of(context)!.lightTheme),
onChanged: controller.switchTheme, onChanged: controller.switchTheme,
), ),
RadioListTile<AdaptiveThemeMode>( RadioListTile<AdaptiveThemeMode>(
groupValue: controller.currentTheme, groupValue: controller.currentTheme,
value: AdaptiveThemeMode.dark, value: AdaptiveThemeMode.dark,
title: Text(L10n.of(context).darkTheme), title: Text(L10n.of(context)!.darkTheme),
onChanged: controller.switchTheme, onChanged: controller.switchTheme,
), ),
const Divider(height: 1), const Divider(height: 1),
ListTile( ListTile(
title: Text( title: Text(
L10n.of(context).wallpaper, L10n.of(context)!.wallpaper,
style: TextStyle( style: TextStyle(
color: Theme.of(context).colorScheme.secondary, color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
@ -99,10 +101,10 @@ class SettingsStyleView extends StatelessWidget {
), ),
Builder(builder: (context) { Builder(builder: (context) {
return ListTile( return ListTile(
title: Text(L10n.of(context).changeWallpaper), title: Text(L10n.of(context)!.changeWallpaper),
trailing: Icon( trailing: Icon(
Icons.photo_outlined, Icons.photo_outlined,
color: Theme.of(context).textTheme.bodyText1.color, color: Theme.of(context).textTheme.bodyText1?.color,
), ),
onTap: controller.setWallpaperAction, onTap: controller.setWallpaperAction,
); );
@ -110,7 +112,7 @@ class SettingsStyleView extends StatelessWidget {
const Divider(height: 1), const Divider(height: 1),
ListTile( ListTile(
title: Text( title: Text(
L10n.of(context).fontSize, L10n.of(context)!.fontSize,
style: TextStyle( style: TextStyle(
color: Theme.of(context).colorScheme.secondary, color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,

View File

@ -71,8 +71,10 @@ class StoryView extends StatelessWidget {
), ),
), ),
), ),
actions: [ actions: currentEvent == null
if (!controller.isOwnStory && currentEvent != null) ? null
: [
if (!controller.isOwnStory)
AnimatedOpacity( AnimatedOpacity(
duration: const Duration(seconds: 1), duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1, opacity: controller.isHold ? 0 : 1,