From 321af571d60637cdcd101daef61a8181b44bfe5e Mon Sep 17 00:00:00 2001 From: LollipopKit <2036293523@qq.com> Date: Tue, 26 Oct 2021 17:27:02 +0800 Subject: [PATCH] Optimized primary color fetching method --- lib/app.dart | 3 ++- lib/data/res/color.dart | 2 +- lib/view/page/setting.dart | 49 ++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index e76116e4..dee263fd 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -49,7 +49,8 @@ class MyApp extends StatelessWidget { focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor))), radioTheme: RadioThemeData( - fillColor: getMaterialStateColor(primaryColor),), + fillColor: getMaterialStateColor(primaryColor), + ), ), darkTheme: ThemeData.dark().copyWith( primaryColor: primaryColor, diff --git a/lib/data/res/color.dart b/lib/data/res/color.dart index 5dbeafe9..e361ba69 100644 --- a/lib/data/res/color.dart +++ b/lib/data/res/color.dart @@ -3,7 +3,7 @@ import 'package:toolbox/core/utils.dart'; import 'package:toolbox/data/store/setting.dart'; import 'package:toolbox/locator.dart'; -Color primaryColor = Color(locator().primaryColor.fetch()!); +Color get primaryColor => Color(locator().primaryColor.fetch()!); class DynamicColor { /// 白天模式显示的颜色 diff --git a/lib/view/page/setting.dart b/lib/view/page/setting.dart index efe186f8..e3e7272d 100644 --- a/lib/view/page/setting.dart +++ b/lib/view/page/setting.dart @@ -15,20 +15,20 @@ class SettingPage extends StatefulWidget { class _SettingPageState extends State { late SettingStore _store; late int _selectedColorValue; - double _value = 0; - late Color _textColor; + double _intervalValue = 0; + late Color priColor; -@override + @override void didChangeDependencies() { super.didChangeDependencies(); - _textColor = Theme.of(context).textTheme.bodyText1!.color!; + priColor = primaryColor; } @override void initState() { super.initState(); _store = locator(); - _value = _store.serverStatusUpdateInterval.fetch()!.toDouble(); + _intervalValue = _store.serverStatusUpdateInterval.fetch()!.toDouble(); } @override @@ -45,36 +45,43 @@ class _SettingPageState extends State { ExpansionTile( tilePadding: EdgeInsets.zero, childrenPadding: EdgeInsets.zero, - title: Text( + textColor: priColor, + title: const Text( 'Server status update interval', - style: TextStyle(fontSize: 14, color: _textColor), + style: TextStyle(fontSize: 14), textAlign: TextAlign.start, ), subtitle: const Text( 'Will take effect the next time app launches.', style: TextStyle(color: Colors.grey), ), - trailing: Text('${_value.toInt()} s'), + trailing: Text('${_intervalValue.toInt()} s'), children: [ Slider( - thumbColor: primaryColor, - activeColor: primaryColor.withOpacity(0.7), + thumbColor: priColor, + activeColor: priColor.withOpacity(0.7), min: 0, max: 10, - value: _value, + value: _intervalValue, onChanged: (newValue) { setState(() { - _value = newValue; + _intervalValue = newValue; }); }, onChangeEnd: (val) => _store.serverStatusUpdateInterval.put(val.toInt()), - label: '${_value.toInt()} seconds', + label: '${_intervalValue.toInt()} seconds', divisions: 10, ), - const SizedBox(height: 3,), - _value == 0.0 ? const Text('You set to 0, will not update automatically.') : const SizedBox(), - const SizedBox(height: 13,) + const SizedBox( + height: 3, + ), + _intervalValue == 0.0 + ? const Text('You set to 0, will not update automatically.') + : const SizedBox(), + const SizedBox( + height: 13, + ) ], ), ) @@ -84,24 +91,24 @@ class _SettingPageState extends State { } Widget _buildAppColorPreview() { - final nowAppColor = _store.primaryColor.fetch()!; return ExpansionTile( + textColor: priColor, tilePadding: EdgeInsets.zero, childrenPadding: EdgeInsets.zero, children: [ - _buildAppColorPicker(Color(nowAppColor)), + _buildAppColorPicker(priColor), _buildColorPickerConfirmBtn() ], trailing: ClipOval( child: Container( - color: Color(nowAppColor), + color: priColor, height: 27, width: 27, ), ), - title: Text( + title: const Text( 'App primary color', - style: TextStyle(fontSize: 14, color: _textColor), + style: TextStyle(fontSize: 14), )); }