feat: add select/deselect icon on Patches Selector View

This commit is contained in:
Alberto Ponces
2022-08-21 02:03:36 +01:00
parent 2f83ea290b
commit b327926219
4 changed files with 50 additions and 18 deletions

View File

@ -40,6 +40,7 @@ class _AppSelectorViewState extends State<AppSelectorView> {
? Column( ? Column(
children: [ children: [
SearchBar( SearchBar(
showSelectIcon: false,
fillColor: fillColor:
isDark ? const Color(0xff1B222B) : Colors.grey[200], isDark ? const Color(0xff1B222B) : Colors.grey[200],
hintText: FlutterI18n.translate( hintText: FlutterI18n.translate(

View File

@ -31,6 +31,7 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
? Column( ? Column(
children: [ children: [
SearchBar( SearchBar(
showSelectIcon: true,
fillColor: fillColor:
isDark ? const Color(0xff1B222B) : Colors.grey[200], isDark ? const Color(0xff1B222B) : Colors.grey[200],
hintText: FlutterI18n.translate( hintText: FlutterI18n.translate(
@ -43,6 +44,7 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
query = searchQuery; query = searchQuery;
}); });
}, },
onSelectAll: (value) => model.selectAllPatches(value),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
query.isEmpty || query.length < 2 query.isEmpty || query.length < 2
@ -90,11 +92,8 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
simpleName: model.patches[index].simpleName, simpleName: model.patches[index].simpleName,
version: model.patches[index].version, version: model.patches[index].version,
description: model.patches[index].description, description: model.patches[index].description,
isSelected: model.selectedPatches.any( isSelected: model.isSelected(index),
(element) => element.name == model.patches[index].name, onChanged: (value) => model.selectPatch(index, value),
),
onChanged: (value) =>
model.selectPatch(model.patches[index].name, value),
); );
_items.add(item); _items.add(item);
return item; return item;
@ -118,11 +117,8 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
simpleName: model.patches[index].simpleName, simpleName: model.patches[index].simpleName,
version: model.patches[index].version, version: model.patches[index].version,
description: model.patches[index].description, description: model.patches[index].description,
isSelected: model.selectedPatches.any( isSelected: model.isSelected(index),
(element) => element.name == model.patches[index].name, onChanged: (value) => model.selectPatch(index, value),
),
onChanged: (value) =>
model.selectPatch(model.patches[index].name, value),
); );
_items.add(item); _items.add(item);
return item; return item;

View File

@ -17,8 +17,14 @@ class PatchesSelectorViewModel extends BaseViewModel {
notifyListeners(); notifyListeners();
} }
void selectPatch(String name, bool isSelected) { bool isSelected(int index) {
Patch patch = patches.firstWhere((p) => p.name == name); 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)) { if (isSelected && !selectedPatches.contains(patch)) {
selectedPatches.add(patch); selectedPatches.add(patch);
} else { } else {
@ -27,6 +33,14 @@ class PatchesSelectorViewModel extends BaseViewModel {
notifyListeners(); notifyListeners();
} }
void selectAllPatches(bool isSelected) {
selectedPatches.clear();
if (isSelected) {
selectedPatches.addAll(patches);
}
notifyListeners();
}
void selectPatches() { void selectPatches() {
locator<PatcherViewModel>().selectedPatches = selectedPatches; locator<PatcherViewModel>().selectedPatches = selectedPatches;
locator<PatcherViewModel>().notifyListeners(); locator<PatcherViewModel>().notifyListeners();

View File

@ -3,17 +3,21 @@ import 'package:google_fonts/google_fonts.dart';
class SearchBar extends StatefulWidget { class SearchBar extends StatefulWidget {
final String? hintText; final String? hintText;
final Color? backgroundColor;
final Color? fillColor; final Color? fillColor;
final bool showSelectIcon;
final Function(bool)? onSelectAll;
final Color? backgroundColor;
final Color? hintTextColor; final Color? hintTextColor;
const SearchBar({ const SearchBar({
Key? key,
required this.hintText, required this.hintText,
required this.fillColor,
required this.onQueryChanged,
this.onSelectAll,
this.showSelectIcon = false,
this.backgroundColor = const Color(0xff1B222B), this.backgroundColor = const Color(0xff1B222B),
this.hintTextColor = Colors.white, this.hintTextColor = Colors.white,
required this.fillColor,
Key? key,
required this.onQueryChanged,
}) : super(key: key); }) : super(key: key);
final Function(String) onQueryChanged; final Function(String) onQueryChanged;
@ -24,6 +28,8 @@ class SearchBar extends StatefulWidget {
class _SearchBarState extends State<SearchBar> { class _SearchBarState extends State<SearchBar> {
final TextEditingController _textController = TextEditingController(); final TextEditingController _textController = TextEditingController();
bool _toggleSelectAll = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
@ -40,7 +46,7 @@ class _SearchBarState extends State<SearchBar> {
child: Row( child: Row(
children: [ children: [
Expanded( Expanded(
child: TextField( child: TextFormField(
onChanged: widget.onQueryChanged, onChanged: widget.onQueryChanged,
controller: _textController, controller: _textController,
decoration: InputDecoration( decoration: InputDecoration(
@ -65,7 +71,22 @@ class _SearchBarState extends State<SearchBar> {
widget.onQueryChanged(''); 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( border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
borderSide: BorderSide.none, borderSide: BorderSide.none,