From 5284ceefd6854e4135bfddfc9d87f3d156d91b62 Mon Sep 17 00:00:00 2001 From: lollipopkit Date: Wed, 27 Dec 2023 11:44:45 +0800 Subject: [PATCH] opt. --- lib/app.dart | 19 ++++------- lib/data/model/app/rebuild.dart | 6 +--- lib/view/page/backup.dart | 5 ++- lib/view/page/home.dart | 6 ++-- lib/view/page/ping.dart | 5 ++- lib/view/page/server/edit.dart | 21 ++++++------ lib/view/page/server/tab.dart | 13 ++++--- lib/view/page/setting/entry.dart | 53 ++++++++++++++--------------- lib/view/widget/appbar.dart | 2 +- lib/view/widget/value_notifier.dart | 16 --------- test/status_prase_test.dart | 2 +- 11 files changed, 59 insertions(+), 89 deletions(-) delete mode 100644 lib/view/widget/value_notifier.dart diff --git a/lib/app.dart b/lib/app.dart index 98e93820d..ae7426b06 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -5,7 +5,6 @@ import 'package:toolbox/core/extension/context/common.dart'; import 'package:toolbox/core/extension/locale.dart'; import 'package:toolbox/data/res/rebuild.dart'; import 'package:toolbox/data/res/store.dart'; -import 'package:toolbox/view/widget/value_notifier.dart'; import 'core/utils/ui.dart'; import 'data/res/build_data.dart'; @@ -27,20 +26,16 @@ class MyApp extends StatelessWidget { } Widget _wrapTheme(BuildContext context) { - return ValueBuilder( + return ListenableBuilder( listenable: RebuildNodes.app, - build: () { + builder: (_, __) { final tMode = Stores.setting.themeMode.fetch(); // Issue #57 - var themeMode = ThemeMode.system; - switch (tMode) { - case 1 || 2: - themeMode = ThemeMode.values[tMode]; - break; - case 3: - themeMode = ThemeMode.dark; - break; - } + final themeMode = switch (tMode) { + 1 || 2 => ThemeMode.values[tMode], + 3 => ThemeMode.dark, + _ => ThemeMode.system, + }; final locale = Stores.setting.locale.fetch().toLocale; final darkTheme = ThemeData( useMaterial3: true, diff --git a/lib/data/model/app/rebuild.dart b/lib/data/model/app/rebuild.dart index 67997775b..5f160865d 100644 --- a/lib/data/model/app/rebuild.dart +++ b/lib/data/model/app/rebuild.dart @@ -1,7 +1,6 @@ import 'package:flutter/foundation.dart'; -// ignore: prefer_void_to_null -class RebuildNode implements ValueListenable { +class RebuildNode implements Listenable { final List _listeners = []; RebuildNode(); @@ -21,7 +20,4 @@ class RebuildNode implements ValueListenable { listener(); } } - - @override - Null get value => null; } diff --git a/lib/view/page/backup.dart b/lib/view/page/backup.dart index 028a873d7..4b278b98e 100644 --- a/lib/view/page/backup.dart +++ b/lib/view/page/backup.dart @@ -21,7 +21,6 @@ import 'package:toolbox/view/widget/expand_tile.dart'; import 'package:toolbox/view/widget/cardx.dart'; import 'package:toolbox/view/widget/input_field.dart'; import 'package:toolbox/view/widget/store_switch.dart'; -import 'package:toolbox/view/widget/value_notifier.dart'; class BackupPage extends StatelessWidget { BackupPage({super.key}); @@ -161,9 +160,9 @@ class BackupPage extends StatelessWidget { ), ListTile( title: Text(l10n.manual), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: webdavLoading, - build: () { + builder: (_, __) { if (webdavLoading.value) { return UIs.centerSizedLoadingSmall; } diff --git a/lib/view/page/home.dart b/lib/view/page/home.dart index 53aeceb50..29be3e1ca 100644 --- a/lib/view/page/home.dart +++ b/lib/view/page/home.dart @@ -29,7 +29,6 @@ import '../../data/res/url.dart'; import '../widget/appbar.dart'; import '../widget/cardx.dart'; import '../widget/url_text.dart'; -import '../widget/value_notifier.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @@ -114,6 +113,7 @@ class _HomePageState extends State return Scaffold( drawer: _buildDrawer(), appBar: CustomAppBar( + centerTitle: false, title: const Text(BuildData.name), actions: [ IconButton( @@ -133,9 +133,9 @@ class _HomePageState extends State } }, ), - bottomNavigationBar: ValueBuilder( + bottomNavigationBar: ListenableBuilder( listenable: _selectIndex, - build: _buildBottomBar, + builder: (_, __) => _buildBottomBar(), ), ); } diff --git a/lib/view/page/ping.dart b/lib/view/page/ping.dart index a7f95aca6..556795e8b 100644 --- a/lib/view/page/ping.dart +++ b/lib/view/page/ping.dart @@ -8,7 +8,6 @@ import 'package:toolbox/core/extension/context/snackbar.dart'; import 'package:toolbox/core/extension/uint8list.dart'; import 'package:toolbox/core/utils/share.dart'; import 'package:toolbox/data/res/provider.dart'; -import 'package:toolbox/view/widget/value_notifier.dart'; import '../../data/model/server/ping_result.dart'; import '../../data/res/color.dart'; @@ -49,9 +48,9 @@ class _PingPageState extends State Widget build(BuildContext context) { super.build(context); return Scaffold( - body: ValueBuilder( + body: ListenableBuilder( listenable: _results, - build: _buildBody, + builder: (_, __) => _buildBody(), ), floatingActionButton: _buildFAB(), ); diff --git a/lib/view/page/server/edit.dart b/lib/view/page/server/edit.dart index 770a5997f..a320f42cb 100644 --- a/lib/view/page/server/edit.dart +++ b/lib/view/page/server/edit.dart @@ -17,7 +17,6 @@ import '../../widget/appbar.dart'; import '../../widget/input_field.dart'; import '../../widget/cardx.dart'; import '../../widget/tag.dart'; -import '../../widget/value_notifier.dart'; class ServerEditPage extends StatefulWidget { const ServerEditPage({super.key, this.spi}); @@ -221,9 +220,9 @@ class _ServerEditPageState extends State { _buildJumpServer(), ListTile( title: Text(l10n.autoConnect), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _autoConnect, - build: () => Switch( + builder: (_, __) => Switch( value: _autoConnect.value, onChanged: (val) { _autoConnect.value = val; @@ -245,9 +244,9 @@ class _ServerEditPageState extends State { Widget _buildAuth() { final switch_ = ListTile( title: Text(l10n.keyAuth), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _keyIdx, - build: () => Switch( + builder: (_, __) => Switch( value: _keyIdx.value != null, onChanged: (val) { if (val) { @@ -261,9 +260,9 @@ class _ServerEditPageState extends State { ); /// Put [switch_] out of [ValueBuilder] to avoid rebuild - return ValueBuilder( + return ListenableBuilder( listenable: _keyIdx, - build: () { + builder: (_, __) { final children = [switch_]; if (_keyIdx.value != null) { children.add(_buildKeyAuth()); @@ -334,9 +333,9 @@ class _ServerEditPageState extends State { } Widget _buildRadio(int index, PrivateKeyInfo pki) { - return ValueBuilder( + return ListenableBuilder( listenable: _keyIdx, - build: () => Radio( + builder: (_, __) => Radio( value: index, groupValue: _keyIdx.value, onChanged: (value) { @@ -347,9 +346,9 @@ class _ServerEditPageState extends State { } Widget _buildJumpServer() { - return ValueBuilder( + return ListenableBuilder( listenable: _jumpServer, - build: () { + builder: (_, __) { final children = Pros.server.servers .where((element) => element.spi.jumpId == null) .where((element) => element.spi.id != widget.spi?.id) diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index 5d1c68b8a..1ddcb7661 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -14,7 +14,6 @@ import 'package:toolbox/data/model/app/shell_func.dart'; import 'package:toolbox/data/model/server/try_limiter.dart'; import 'package:toolbox/data/res/provider.dart'; import 'package:toolbox/data/res/store.dart'; -import 'package:toolbox/view/widget/value_notifier.dart'; import '../../../core/route.dart'; import '../../../data/model/app/net_view.dart'; @@ -61,9 +60,9 @@ class _ServerPageState extends State super.build(context); return Scaffold( appBar: _buildTagsSwitcher(Pros.server), - body: ValueBuilder( + body: ListenableBuilder( listenable: Stores.setting.textFactor.listenable(), - build: () { + builder: (_, __) { _textFactorDouble = Stores.setting.textFactor.fetch(); _textFactor = TextScaler.linear(_textFactorDouble); return _buildBody(); @@ -200,9 +199,9 @@ class _ServerPageState extends State final cardStatus = _getCardNoti(id); final title = _buildServerCardTitle(srv.status, srv.state, srv.spi); - return ValueBuilder( + return ListenableBuilder( listenable: cardStatus, - build: () { + builder: (_, __) { late final List children; if (srv.state == ServerState.finished) { if (cardStatus.value.flip) { @@ -417,9 +416,9 @@ class _ServerPageState extends State Widget _buildDisk(ServerStatus ss, String id) { final cardNoti = _getCardNoti(id); - return ValueBuilder( + return ListenableBuilder( listenable: cardNoti, - build: () { + builder: (_, __) { final rootDisk = findRootDisk(ss.disk); final isSpeed = cardNoti.value.diskIO ?? !Stores.setting.serverTabPreferDiskAmount.fetch(); diff --git a/lib/view/page/setting/entry.dart b/lib/view/page/setting/entry.dart index 163f7a9a8..f4a18c67b 100644 --- a/lib/view/page/setting/entry.dart +++ b/lib/view/page/setting/entry.dart @@ -34,7 +34,6 @@ import '../../widget/appbar.dart'; import '../../widget/input_field.dart'; import '../../widget/cardx.dart'; import '../../widget/store_switch.dart'; -import '../../widget/value_notifier.dart'; class SettingPage extends StatefulWidget { const SettingPage({super.key}); @@ -285,9 +284,9 @@ class _SettingPageState extends State { onTap: () { _updateIntervalKey.currentState?.showButtonMenu(); }, - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _updateInterval, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _updateIntervalKey, itemBuilder: (_) => items, initialValue: _updateInterval.value, @@ -311,9 +310,9 @@ class _SettingPageState extends State { Widget _buildAppColor() { return ListTile( trailing: ClipOval( - child: ValueBuilder( + child: ListenableBuilder( listenable: _selectedColorValue, - build: () => Container( + builder: (_, __) => Container( color: primaryColor, height: 27, width: 27, @@ -444,8 +443,8 @@ class _SettingPageState extends State { onTap: () { _maxRetryKey.currentState?.showButtonMenu(); }, - trailing: ValueBuilder( - build: () => PopupMenuButton( + trailing: ListenableBuilder( + builder: (_, __) => PopupMenuButton( key: _maxRetryKey, itemBuilder: (BuildContext context) => items, initialValue: _maxRetryCount.value, @@ -490,9 +489,9 @@ class _SettingPageState extends State { onTap: () { _themeKey.currentState?.showButtonMenu(); }, - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _nightMode, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _themeKey, itemBuilder: (BuildContext context) => items, initialValue: _nightMode.value, @@ -577,9 +576,9 @@ class _SettingPageState extends State { } Widget _buildTermFontSize() { - return ValueBuilder( + return ListenableBuilder( listenable: _termFontSize, - build: () => ListTile( + builder: (_, __) => ListTile( title: Text(l10n.fontSize), trailing: Text( _termFontSize.value.toString(), @@ -641,9 +640,9 @@ class _SettingPageState extends State { onTap: () { _localeKey.currentState?.showButtonMenu(); }, - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _localeCode, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _localeKey, itemBuilder: (BuildContext context) => items, initialValue: _localeCode.value, @@ -681,9 +680,9 @@ class _SettingPageState extends State { ).toList(); return ListTile( title: Text('${l10n.light} ${l10n.theme.toLowerCase()}'), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _editorTheme, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _editorThemeKey, itemBuilder: (BuildContext context) => items, initialValue: _editorTheme.value, @@ -714,9 +713,9 @@ class _SettingPageState extends State { ).toList(); return ListTile( title: Text('${l10n.dark} ${l10n.theme.toLowerCase()}'), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _editorDarkTheme, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _editorDarkThemeKey, itemBuilder: (BuildContext context) => items, initialValue: _editorDarkTheme.value, @@ -768,9 +767,9 @@ class _SettingPageState extends State { onTap: () { _rotateQuarterKey.currentState?.showButtonMenu(); }, - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _rotateQuarter, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _rotateQuarterKey, itemBuilder: (BuildContext context) => items, initialValue: _rotateQuarter.value, @@ -816,9 +815,9 @@ class _SettingPageState extends State { return ListTile( title: Text(l10n.keyboardType), subtitle: Text(l10n.keyboardCompatibility, style: UIs.textGrey), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _keyboardType, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _keyboardTypeKey, itemBuilder: (BuildContext context) => items, initialValue: _keyboardType.value, @@ -872,9 +871,9 @@ class _SettingPageState extends State { .toList(); return ListTile( title: Text(l10n.netViewType), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _netViewType, - build: () => PopupMenuButton( + builder: (_, __) => PopupMenuButton( key: _netViewTypeKey, itemBuilder: (BuildContext context) => items, initialValue: _netViewType.value, @@ -942,9 +941,9 @@ class _SettingPageState extends State { return ListTile( title: Text(l10n.textScaler), subtitle: Text(l10n.textScalerTip, style: UIs.textGrey), - trailing: ValueBuilder( + trailing: ListenableBuilder( listenable: _textScaler, - build: () => Text( + builder: (_, __) => Text( _textScaler.value.toString(), style: UIs.text15, ), @@ -1013,9 +1012,9 @@ class _SettingPageState extends State { } Widget _buildEditorFontSize() { - return ValueBuilder( + return ListenableBuilder( listenable: _editorFontSize, - build: () => ListTile( + builder: (_, __) => ListTile( title: Text(l10n.fontSize), trailing: Text( _editorFontSize.value.toString(), diff --git a/lib/view/widget/appbar.dart b/lib/view/widget/appbar.dart index b149e79e4..05890aac3 100644 --- a/lib/view/widget/appbar.dart +++ b/lib/view/widget/appbar.dart @@ -11,7 +11,7 @@ class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { super.key, this.title, this.actions, - this.centerTitle, + this.centerTitle = true, this.leading, this.backgroundColor, }); diff --git a/lib/view/widget/value_notifier.dart b/lib/view/widget/value_notifier.dart deleted file mode 100644 index 423b8e853..000000000 --- a/lib/view/widget/value_notifier.dart +++ /dev/null @@ -1,16 +0,0 @@ -import 'package:flutter/foundation.dart'; -import 'package:flutter/material.dart'; - -class ValueBuilder extends ValueListenableBuilder { - final ValueListenable listenable; - final Widget Function() build; - - ValueBuilder({ - super.key, - required this.listenable, - required this.build, - }) : super( - valueListenable: listenable, - builder: (_, __, ___) => build(), - ); -} diff --git a/test/status_prase_test.dart b/test/status_prase_test.dart index 70b46c8fa..22b934cfe 100644 --- a/test/status_prase_test.dart +++ b/test/status_prase_test.dart @@ -81,7 +81,7 @@ Overlay 3.0T 1.4t 1.6T 48%/Share/CacheDev1_data/Container/Container-SATA/LIB/DOC /dev/mapper/vg1-snap10016 3.0T 1.4T 1.6T 48% /mnt/snapshot/1/10016 '''; - final disks = parseDisk(raw); + final disks = Disk.parse(raw); print(disks.map((e) => '${e.mount} ${e.used}').join('\n')); }); }