From 614ffcdc25e053bcac4d2f89c56e32b3ba788a32 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 5 Nov 2019 20:37:43 +0200 Subject: [PATCH] Settings --- lib/constants.dart | 1 + lib/main.dart | 10 ++--- lib/redux/app/app_state.dart | 2 +- lib/ui/app/forms/color_picker.dart | 47 +++++++++++++------- lib/ui/settings/device_settings_list.dart | 1 + lib/ui/settings/device_settings_list_vm.dart | 1 + lib/utils/colors.dart | 8 +++- 7 files changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 8652b8dfc..5933b9281 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -236,6 +236,7 @@ const String kExpenseStatusInvoiced = '3'; const String kDefaultCurrencyId = '1'; const String kDefaultDateFormat = '5'; +const String kDefaultAccentColor = '#FF40C4FF'; const String kActivityEmailInvoice = '6'; diff --git a/lib/main.dart b/lib/main.dart index 42bddb379..ec1588f67 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -65,7 +65,7 @@ void main({bool isTesting = false}) async { final prefs = await SharedPreferences.getInstance(); final enableDarkMode = prefs.getBool(kSharedPrefEnableDarkMode) ?? true; - final accentColor = prefs.getString(kSharedPrefAccentColor) ?? '#FF0000'; + final accentColor = prefs.getString(kSharedPrefAccentColor) ?? kDefaultAccentColor; final longPressSelectionIsDefault = prefs.getBool(kSharedPrefLongPressSelectionIsDefault) ?? false; @@ -267,13 +267,11 @@ class InvoiceNinjaAppState extends State { ? ThemeData( brightness: Brightness.dark, accentColor: - convertHexStringToColor(state.uiState.accentColor) ?? - Colors.lightBlueAccent, + convertHexStringToColor(state.uiState.accentColor), ) : ThemeData().copyWith( - accentColor: state.uiState.accentColor == null - ? null - : convertHexStringToColor(state.uiState.accentColor), + accentColor: + convertHexStringToColor(state.uiState.accentColor), primaryColor: const Color(0xFF117cc1), primaryColorLight: const Color(0xFF5dabf4), primaryColorDark: const Color(0xFF0D5D91), diff --git a/lib/redux/app/app_state.dart b/lib/redux/app/app_state.dart index 8045dd48e..d23fe85c0 100644 --- a/lib/redux/app/app_state.dart +++ b/lib/redux/app/app_state.dart @@ -349,7 +349,7 @@ abstract class AppState implements Built { @override String toString() { //return 'Custom fields [UI]: ${uiState.settingsUIState.userCompany.company.customFields}, [DB] ${selectedCompany.customFields}'; - return 'custom fields: ${uiState.settingsUIState.userCompany.company.customFields}'; + return 'color: ${uiState.accentColor}'; //return 'defaultInvoiceDesignId: ${selectedCompany.settings.defaultInvoiceDesignId}'; //return 'Routes: Current: ${uiState.currentRoute} Prev: ${uiState.previousRoute}'; //return 'Route: ${uiState.currentRoute}, Setting Type: ${uiState.settingsUIState.entityType}, Name: ${uiState.settingsUIState.settings.name}, Updated: ${uiState.settingsUIState.updatedAt}'; diff --git a/lib/ui/app/forms/color_picker.dart b/lib/ui/app/forms/color_picker.dart index 16f0bf942..ef6e772db 100644 --- a/lib/ui/app/forms/color_picker.dart +++ b/lib/ui/app/forms/color_picker.dart @@ -4,11 +4,17 @@ import 'package:invoiceninja_flutter/utils/colors.dart'; import 'package:invoiceninja_flutter/utils/localization.dart'; class FormColorPicker extends StatefulWidget { - const FormColorPicker({this.labelText, this.initialValue, this.onSelected}); + const FormColorPicker({ + this.labelText, + this.initialValue, + this.onSelected, + this.showClear = true, + }); final String labelText; final String initialValue; final Function(String) onSelected; + final bool showClear; @override _FormColorPickerState createState() => _FormColorPickerState(); @@ -27,7 +33,7 @@ class _FormColorPickerState extends State { @override void didChangeDependencies() { - _textController.text = widget.initialValue; + _selectedColor = _textController.text = widget.initialValue; super.didChangeDependencies(); } @@ -56,8 +62,7 @@ class _FormColorPickerState extends State { child: BlockPicker( pickerColor: color, onColorChanged: (color) { - final hex = color.value.toRadixString(16); - _pendingColor = '#' + hex.substring(2, hex.length); + _pendingColor = convertColorToHexString(color); }, ), ), @@ -98,18 +103,23 @@ class _FormColorPickerState extends State { Row( mainAxisAlignment: MainAxisAlignment.end, children: [ - Container( - color: convertHexStringToColor(widget.initialValue), - width: 100, - height: 20, + GestureDetector( + onTap: _showPicker, + child: Container( + decoration: BoxDecoration( + color: widget.initialValue == null + ? Colors.grey + : convertHexStringToColor(widget.initialValue), + border: Border.all( + color: Colors.black38, + ), + ), + width: 100, + height: 25, + ), ), - SizedBox(width: 20), - if (_selectedColor == null) - IconButton( - icon: Icon(Icons.color_lens), - onPressed: _showPicker, - ) - else + SizedBox(width: 10), + if (widget.showClear && _selectedColor != null) IconButton( icon: Icon(Icons.clear), onPressed: () { @@ -119,7 +129,12 @@ class _FormColorPickerState extends State { }); widget.onSelected(null); }, - ), + ) + else + IconButton( + icon: Icon(Icons.color_lens), + onPressed: _showPicker, + ) ], ), ], diff --git a/lib/ui/settings/device_settings_list.dart b/lib/ui/settings/device_settings_list.dart index 9f2d99000..71ebd8737 100644 --- a/lib/ui/settings/device_settings_list.dart +++ b/lib/ui/settings/device_settings_list.dart @@ -49,6 +49,7 @@ class _DeviceSettingsState extends State { FormColorPicker( labelText: localization.accentColor, initialValue: uiState.accentColor, + showClear: false, onSelected: (value) => viewModel.onAccentColorChanged(context, value), ), diff --git a/lib/ui/settings/device_settings_list_vm.dart b/lib/ui/settings/device_settings_list_vm.dart index b5b9d2bcd..bb6b0e35e 100644 --- a/lib/ui/settings/device_settings_list_vm.dart +++ b/lib/ui/settings/device_settings_list_vm.dart @@ -97,6 +97,7 @@ class DeviceSettingsVM { AppBuilder.of(context).rebuild(); }, onAccentColorChanged: (BuildContext context, String value) async { + value ??= kDefaultAccentColor; final SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString(kSharedPrefAccentColor, value); store.dispatch(UserSettingsChanged(accentColor: value)); diff --git a/lib/utils/colors.dart b/lib/utils/colors.dart index 7da685b1e..efbf10da0 100644 --- a/lib/utils/colors.dart +++ b/lib/utils/colors.dart @@ -1,8 +1,14 @@ import 'package:flutter/material.dart'; Color convertHexStringToColor(String value) { - if (value == null) + if (value == null) { return null; + } value = value.replaceAll('#', ''); return Color(int.parse(value, radix: 16) + 0xFF000000); } + +String convertColorToHexString(Color color) { + final hex = color.value.toRadixString(16); + return '#' + hex.substring(2, hex.length); +}