From b327926219b9fa50c4adede90279694402467961 Mon Sep 17 00:00:00 2001 From: Alberto Ponces Date: Sun, 21 Aug 2022 02:03:36 +0100 Subject: [PATCH] feat: add select/deselect icon on Patches Selector View --- .../views/app_selector/app_selector_view.dart | 1 + .../patches_selector_view.dart | 16 ++++----- .../patches_selector_viewmodel.dart | 18 ++++++++-- lib/ui/widgets/search_bar.dart | 33 +++++++++++++++---- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lib/ui/views/app_selector/app_selector_view.dart b/lib/ui/views/app_selector/app_selector_view.dart index 0dd2208e..be7fd253 100644 --- a/lib/ui/views/app_selector/app_selector_view.dart +++ b/lib/ui/views/app_selector/app_selector_view.dart @@ -40,6 +40,7 @@ class _AppSelectorViewState extends State { ? Column( children: [ SearchBar( + showSelectIcon: false, fillColor: isDark ? const Color(0xff1B222B) : Colors.grey[200], hintText: FlutterI18n.translate( diff --git a/lib/ui/views/patches_selector/patches_selector_view.dart b/lib/ui/views/patches_selector/patches_selector_view.dart index 3a742a1e..2a240585 100644 --- a/lib/ui/views/patches_selector/patches_selector_view.dart +++ b/lib/ui/views/patches_selector/patches_selector_view.dart @@ -31,6 +31,7 @@ class _PatchesSelectorViewState extends State { ? Column( children: [ SearchBar( + showSelectIcon: true, fillColor: isDark ? const Color(0xff1B222B) : Colors.grey[200], hintText: FlutterI18n.translate( @@ -43,6 +44,7 @@ class _PatchesSelectorViewState extends State { query = searchQuery; }); }, + onSelectAll: (value) => model.selectAllPatches(value), ), const SizedBox(height: 12), query.isEmpty || query.length < 2 @@ -90,11 +92,8 @@ class _PatchesSelectorViewState extends State { simpleName: model.patches[index].simpleName, version: model.patches[index].version, description: model.patches[index].description, - isSelected: model.selectedPatches.any( - (element) => element.name == model.patches[index].name, - ), - onChanged: (value) => - model.selectPatch(model.patches[index].name, value), + isSelected: model.isSelected(index), + onChanged: (value) => model.selectPatch(index, value), ); _items.add(item); return item; @@ -118,11 +117,8 @@ class _PatchesSelectorViewState extends State { simpleName: model.patches[index].simpleName, version: model.patches[index].version, description: model.patches[index].description, - isSelected: model.selectedPatches.any( - (element) => element.name == model.patches[index].name, - ), - onChanged: (value) => - model.selectPatch(model.patches[index].name, value), + isSelected: model.isSelected(index), + onChanged: (value) => model.selectPatch(index, value), ); _items.add(item); return item; diff --git a/lib/ui/views/patches_selector/patches_selector_viewmodel.dart b/lib/ui/views/patches_selector/patches_selector_viewmodel.dart index 960ef4ba..e0c43892 100644 --- a/lib/ui/views/patches_selector/patches_selector_viewmodel.dart +++ b/lib/ui/views/patches_selector/patches_selector_viewmodel.dart @@ -17,8 +17,14 @@ class PatchesSelectorViewModel extends BaseViewModel { notifyListeners(); } - void selectPatch(String name, bool isSelected) { - Patch patch = patches.firstWhere((p) => p.name == name); + bool isSelected(int index) { + return selectedPatches.any( + (element) => element.name == patches[index].name, + ); + } + + void selectPatch(int index, bool isSelected) { + Patch patch = patches.firstWhere((p) => p.name == patches[index].name); if (isSelected && !selectedPatches.contains(patch)) { selectedPatches.add(patch); } else { @@ -27,6 +33,14 @@ class PatchesSelectorViewModel extends BaseViewModel { notifyListeners(); } + void selectAllPatches(bool isSelected) { + selectedPatches.clear(); + if (isSelected) { + selectedPatches.addAll(patches); + } + notifyListeners(); + } + void selectPatches() { locator().selectedPatches = selectedPatches; locator().notifyListeners(); diff --git a/lib/ui/widgets/search_bar.dart b/lib/ui/widgets/search_bar.dart index 1b5724d2..22772a10 100644 --- a/lib/ui/widgets/search_bar.dart +++ b/lib/ui/widgets/search_bar.dart @@ -3,17 +3,21 @@ import 'package:google_fonts/google_fonts.dart'; class SearchBar extends StatefulWidget { final String? hintText; - final Color? backgroundColor; final Color? fillColor; + final bool showSelectIcon; + final Function(bool)? onSelectAll; + final Color? backgroundColor; final Color? hintTextColor; const SearchBar({ + Key? key, required this.hintText, + required this.fillColor, + required this.onQueryChanged, + this.onSelectAll, + this.showSelectIcon = false, this.backgroundColor = const Color(0xff1B222B), this.hintTextColor = Colors.white, - required this.fillColor, - Key? key, - required this.onQueryChanged, }) : super(key: key); final Function(String) onQueryChanged; @@ -24,6 +28,8 @@ class SearchBar extends StatefulWidget { class _SearchBarState extends State { final TextEditingController _textController = TextEditingController(); + bool _toggleSelectAll = false; + @override Widget build(BuildContext context) { return Container( @@ -40,7 +46,7 @@ class _SearchBarState extends State { child: Row( children: [ Expanded( - child: TextField( + child: TextFormField( onChanged: widget.onQueryChanged, controller: _textController, decoration: InputDecoration( @@ -65,7 +71,22 @@ class _SearchBarState extends State { widget.onQueryChanged(''); }, ) - : null, + : widget.showSelectIcon + ? IconButton( + icon: _toggleSelectAll + ? const Icon(Icons.deselect) + : const Icon(Icons.select_all), + iconSize: 24.0, + onPressed: widget.onSelectAll != null + ? () { + setState(() { + _toggleSelectAll = !_toggleSelectAll; + }); + widget.onSelectAll!(_toggleSelectAll); + } + : () => {}, + ) + : null, border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none,