From 3426d707fe058201908c4c2e332bfd14334d9555 Mon Sep 17 00:00:00 2001 From: nyne Date: Mon, 13 Oct 2025 20:12:47 +0800 Subject: [PATCH] Refactor radio button implementations to use RadioGroup. --- analysis_options.yaml | 2 +- lib/pages/comic_details_page/actions.dart | 108 +++++++++--------- lib/pages/home_page.dart | 92 +++++++-------- .../image_favorites_page.dart | 32 +++--- lib/pages/local_comics_page.dart | 56 ++++----- lib/pages/settings/app.dart | 44 ++++--- lib/pages/settings/network.dart | 66 +++++------ 7 files changed, 188 insertions(+), 212 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index d9ed12b..d79c394 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -23,7 +23,7 @@ linter: rules: collection_methods_unrelated_type: false use_build_context_synchronously: false - # avoid_print: false # Uncomment to disable the `avoid_print` rule + avoid_print: false # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule # Additional information about this file can be found at diff --git a/lib/pages/comic_details_page/actions.dart b/lib/pages/comic_details_page/actions.dart index 428c872..4360e08 100644 --- a/lib/pages/comic_details_page/actions.dart +++ b/lib/pages/comic_details_page/actions.dart @@ -155,64 +155,60 @@ abstract mixin class _ComicPageActions { builder: (context, setState) { return ContentDialog( title: "Download".tl, - content: Column( - mainAxisSize: MainAxisSize.min, - children: [ - RadioListTile( - value: -1, - groupValue: selected, - title: Text("Normal".tl), - onChanged: (v) { - setState(() { - selected = v!; - }); - }, - ), - ExpansionTile( - title: Text("Archive".tl), - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.zero, + content: RadioGroup( + groupValue: selected, + onChanged: (v) { + setState(() { + selected = v ?? selected; + }); + }, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + RadioListTile( + value: -1, + title: Text("Normal".tl), ), - collapsedShape: const RoundedRectangleBorder( - borderRadius: BorderRadius.zero, - ), - onExpansionChanged: (b) { - if (!isLoading && b && archives == null) { - isLoading = true; - comicSource.archiveDownloader! - .getArchives(comic.id) - .then((value) { - if (value.success) { - archives = value.data; - } else { - App.rootContext - .showMessage(message: value.errorMessage!); - } - setState(() { - isLoading = false; + ExpansionTile( + title: Text("Archive".tl), + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.zero, + ), + collapsedShape: const RoundedRectangleBorder( + borderRadius: BorderRadius.zero, + ), + onExpansionChanged: (b) { + if (!isLoading && b && archives == null) { + isLoading = true; + comicSource.archiveDownloader! + .getArchives(comic.id) + .then((value) { + if (value.success) { + archives = value.data; + } else { + App.rootContext + .showMessage(message: value.errorMessage!); + } + setState(() { + isLoading = false; + }); }); - }); - } - }, - children: [ - if (archives == null) - const ListLoadingIndicator().toCenter() - else - for (int i = 0; i < archives!.length; i++) - RadioListTile( - value: i, - groupValue: selected, - onChanged: (v) { - setState(() { - selected = v!; - }); - }, - title: Text(archives![i].title), - subtitle: Text(archives![i].description), - ) - ], - ) - ], + } + }, + children: [ + if (archives == null) + const ListLoadingIndicator().toCenter() + else + for (int i = 0; i < archives!.length; i++) + RadioListTile( + value: i, + title: Text(archives![i].title), + subtitle: Text(archives![i].description), + ) + ], + ) + ], + ), ), actions: [ Button.filled( diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index 4b0b2d7..c28c1bb 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -514,51 +514,53 @@ class _ImportComicsWidgetState extends State<_ImportComicsWidget> { child: CircularProgressIndicator(), ), ) - : Column( - key: key, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const SizedBox(width: 600), - ...List.generate(importMethods.length, (index) { - return RadioListTile( - title: Text(importMethods[index]), - value: index, - groupValue: type, - onChanged: (value) { - setState(() { - type = value as int; - }); - }, - ); - }), - if (type != 4) - ListTile( - title: Text("Add to favorites".tl), - trailing: Select( - current: selectedFolder, - values: folders, - minWidth: 112, - onTap: (v) { - setState(() { - selectedFolder = folders[v]; - }); - }, - ), - ).paddingHorizontal(8), - if (!App.isIOS && !App.isMacOS && type != 2 && type != 3) - CheckboxListTile( - enabled: true, - title: Text("Copy to app local path".tl), - value: copyToLocalFolder, - onChanged: (v) { - setState(() { - copyToLocalFolder = !copyToLocalFolder; - }); - }).paddingHorizontal(8), - const SizedBox(height: 8), - Text(info).paddingHorizontal(24), - ], - ), + : RadioGroup( + groupValue: type, + onChanged: (value) { + setState(() { + type = value ?? type; + }); + }, + child: Column( + key: key, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const SizedBox(width: 600), + ...List.generate(importMethods.length, (index) { + return RadioListTile( + title: Text(importMethods[index]), + value: index, + ); + }), + if (type != 4) + ListTile( + title: Text("Add to favorites".tl), + trailing: Select( + current: selectedFolder, + values: folders, + minWidth: 112, + onTap: (v) { + setState(() { + selectedFolder = folders[v]; + }); + }, + ), + ).paddingHorizontal(8), + if (!App.isIOS && !App.isMacOS && type != 2 && type != 3) + CheckboxListTile( + enabled: true, + title: Text("Copy to app local path".tl), + value: copyToLocalFolder, + onChanged: (v) { + setState(() { + copyToLocalFolder = !copyToLocalFolder; + }); + }).paddingHorizontal(8), + const SizedBox(height: 8), + Text(info).paddingHorizontal(24), + ], + ), + ), actions: [ Button.text( child: Row( diff --git a/lib/pages/image_favorites_page/image_favorites_page.dart b/lib/pages/image_favorites_page/image_favorites_page.dart index ca0c4f1..5b32997 100644 --- a/lib/pages/image_favorites_page/image_favorites_page.dart +++ b/lib/pages/image_favorites_page/image_favorites_page.dart @@ -404,21 +404,23 @@ class _ImageFavoritesDialogState extends State<_ImageFavoritesDialog> { children: [ tabBar, TabViewBody(children: [ - Column( - children: ImageFavoriteSortType.values - .map( - (e) => RadioListTile( - title: Text(e.value.tl), - value: e, - groupValue: sortType, - onChanged: (v) { - setState(() { - sortType = v!; - }); - }, - ), - ) - .toList(), + RadioGroup( + groupValue: sortType, + onChanged: (v) { + setState(() { + sortType = v ?? sortType; + }); + }, + child: Column( + children: ImageFavoriteSortType.values + .map( + (e) => RadioListTile( + title: Text(e.value.tl), + value: e, + ), + ) + .toList(), + ), ), Column( children: [ diff --git a/lib/pages/local_comics_page.dart b/lib/pages/local_comics_page.dart index 8c8ab24..e4af827 100644 --- a/lib/pages/local_comics_page.dart +++ b/lib/pages/local_comics_page.dart @@ -70,39 +70,29 @@ class _LocalComicsPageState extends State { return StatefulBuilder(builder: (context, setState) { return ContentDialog( title: "Sort".tl, - content: Column( - children: [ - RadioListTile( - title: Text("Name".tl), - value: LocalSortType.name, - groupValue: sortType, - onChanged: (v) { - setState(() { - sortType = v!; - }); - }, - ), - RadioListTile( - title: Text("Date".tl), - value: LocalSortType.timeAsc, - groupValue: sortType, - onChanged: (v) { - setState(() { - sortType = v!; - }); - }, - ), - RadioListTile( - title: Text("Date Desc".tl), - value: LocalSortType.timeDesc, - groupValue: sortType, - onChanged: (v) { - setState(() { - sortType = v!; - }); - }, - ), - ], + content: RadioGroup( + groupValue: sortType, + onChanged: (v) { + setState(() { + sortType = v ?? sortType; + }); + }, + child: Column( + children: [ + RadioListTile( + title: Text("Name".tl), + value: LocalSortType.name, + ), + RadioListTile( + title: Text("Date".tl), + value: LocalSortType.timeAsc, + ), + RadioListTile( + title: Text("Date Desc".tl), + value: LocalSortType.timeDesc, + ), + ], + ), ), actions: [ FilledButton( diff --git a/lib/pages/settings/app.dart b/lib/pages/settings/app.dart index c057eb3..217edc3 100644 --- a/lib/pages/settings/app.dart +++ b/lib/pages/settings/app.dart @@ -428,30 +428,26 @@ class _WebdavSettingState extends State<_WebdavSetting> { ), ), const SizedBox(height: 12), - Row( - children: [ - Text("Operation".tl), - Radio( - groupValue: upload, - value: true, - onChanged: (value) { - setState(() { - upload = value!; - }); - }, - ), - Text("Upload".tl), - Radio( - groupValue: upload, - value: false, - onChanged: (value) { - setState(() { - upload = value!; - }); - }, - ), - Text("Download".tl), - ], + RadioGroup( + groupValue: upload, + onChanged: (value) { + setState(() { + upload = value ?? upload; + }); + }, + child: Row( + children: [ + Text("Operation".tl), + Radio( + value: true, + ), + Text("Upload".tl), + Radio( + value: false, + ), + Text("Download".tl), + ], + ), ), const SizedBox(height: 16), AnimatedSize( diff --git a/lib/pages/settings/network.dart b/lib/pages/settings/network.dart index 9f80afd..a97d02e 100644 --- a/lib/pages/settings/network.dart +++ b/lib/pages/settings/network.dart @@ -111,44 +111,34 @@ class _ProxySettingViewState extends State<_ProxySettingView> { return PopUpWidgetScaffold( title: "Proxy".tl, body: SingleChildScrollView( - child: Column( - children: [ - RadioListTile( - title: Text("Direct".tl), - value: 'direct', - groupValue: type, - onChanged: (v) { - setState(() { - type = v!; - }); - appdata.settings['proxy'] = toProxyStr(); - appdata.saveData(); - }, - ), - RadioListTile( - title: Text("System".tl), - value: 'system', - groupValue: type, - onChanged: (v) { - setState(() { - type = v!; - }); - appdata.settings['proxy'] = toProxyStr(); - appdata.saveData(); - }, - ), - RadioListTile( - title: Text("Manual".tl), - value: 'manual', - groupValue: type, - onChanged: (v) { - setState(() { - type = v!; - }); - }, - ), - if (type == 'manual') buildManualProxy(), - ], + child: RadioGroup( + groupValue: type, + onChanged: (v) { + setState(() { + type = v ?? type; + }); + if (type != 'manual') { + appdata.settings['proxy'] = toProxyStr(); + appdata.saveData(); + } + }, + child: Column( + children: [ + RadioListTile( + title: Text("Direct".tl), + value: 'direct', + ), + RadioListTile( + title: Text("System".tl), + value: 'system', + ), + RadioListTile( + title: Text("Manual".tl), + value: 'manual', + ), + if (type == 'manual') buildManualProxy(), + ], + ), ), ), );