From 66eea9d69033d497c740331fa42daee338df3b31 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 14 Sep 2023 11:40:48 +0300 Subject: [PATCH] checkbox picker refactoring --- .../widgets/present_provider_picker.dart | 1 + lib/src/widgets/check_box_picker.dart | 111 ++++++++---------- lib/store/check_box_picker_store.dart | 15 +++ 3 files changed, 63 insertions(+), 64 deletions(-) create mode 100644 lib/store/check_box_picker_store.dart diff --git a/lib/src/screens/exchange/widgets/present_provider_picker.dart b/lib/src/screens/exchange/widgets/present_provider_picker.dart index 75429aea9..7799fc154 100644 --- a/lib/src/screens/exchange/widgets/present_provider_picker.dart +++ b/lib/src/screens/exchange/widgets/present_provider_picker.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/view_model/exchange/exchange_view_model.dart'; +import 'package:cake_wallet/store/check_box_picker_store.dart'; class PresentProviderPicker extends StatelessWidget { PresentProviderPicker({required this.exchangeViewModel}); diff --git a/lib/src/widgets/check_box_picker.dart b/lib/src/widgets/check_box_picker.dart index a52f8bdfe..3fa91936f 100644 --- a/lib/src/widgets/check_box_picker.dart +++ b/lib/src/widgets/check_box_picker.dart @@ -1,13 +1,13 @@ +import 'package:cake_wallet/src/widgets/picker_wrapper_widget.dart'; import 'package:cake_wallet/src/widgets/standard_checkbox.dart'; +import 'package:cake_wallet/store/check_box_picker_store.dart'; import 'package:cake_wallet/themes/extensions/cake_text_theme.dart'; -import 'package:cake_wallet/palette.dart'; +import 'package:cake_wallet/themes/extensions/picker_theme.dart'; import 'package:cake_wallet/utils/responsive_layout_util.dart'; import 'package:flutter/material.dart'; -import 'package:cake_wallet/src/widgets/picker_wrapper_widget.dart'; -import 'package:cake_wallet/themes/extensions/filter_theme.dart'; -import 'package:cake_wallet/themes/extensions/picker_theme.dart'; +import 'package:flutter_mobx/flutter_mobx.dart'; -class CheckBoxPicker extends StatefulWidget { +class CheckBoxPicker extends StatelessWidget { CheckBoxPicker({ required this.items, required this.onChanged, @@ -22,26 +22,17 @@ class CheckBoxPicker extends StatefulWidget { final bool isSeparated; final Function(int, bool) onChanged; - @override - CheckBoxPickerState createState() => CheckBoxPickerState(items); -} - -class CheckBoxPickerState extends State { - CheckBoxPickerState(this.items); - - final List items; - - ScrollController controller = ScrollController(); + final ScrollController _controller = ScrollController(); @override Widget build(BuildContext context) { return PickerWrapperWidget( children: [ - if (widget.title.isNotEmpty) + if (title.isNotEmpty) Container( padding: EdgeInsets.symmetric(horizontal: 24), child: Text( - widget.title, + title, textAlign: TextAlign.center, style: TextStyle( fontSize: 18, @@ -72,10 +63,10 @@ class CheckBoxPickerState extends State { children: [ items.length > 3 ? Scrollbar( - controller: controller, - child: itemsList(), + controller: _controller, + child: itemsList(context), ) - : itemsList(), + : itemsList(context), ], ), ), @@ -89,31 +80,32 @@ class CheckBoxPickerState extends State { ); } - Widget itemsList() { + Widget itemsList(BuildContext context) { return Container( color: Theme.of(context).extension()!.dividerColor, child: ListView.separated( padding: EdgeInsets.zero, - controller: controller, + controller: _controller, shrinkWrap: true, - separatorBuilder: (context, index) => widget.isSeparated + separatorBuilder: (context, index) => isSeparated ? Divider( - color: Theme.of(context).extension()!.dividerColor, - height: 1, - ) + color: Theme.of(context).extension()!.dividerColor, + height: 1, + ) : const SizedBox(), - itemCount: items == null || items.isEmpty ? 0 : items.length, - itemBuilder: (context, index) => buildItem(index), + itemCount: items.isEmpty ? 0 : items.length, + itemBuilder: (context, index) => buildItem(context, index), ), ); } - Widget buildItem(int index) { + Widget buildItem(BuildContext context, int index) { final item = items[index]; return GestureDetector( onTap: () { - Navigator.of(context).pop(); + item.value = !item.value; + onChanged(index, item.value); }, child: Container( height: 55, @@ -121,46 +113,37 @@ class CheckBoxPickerState extends State { padding: EdgeInsets.only(left: 24, right: 24), child: Row( children: [ - StandardCheckbox( - value: item.value, - gradientBackground: true, - borderColor: Theme.of(context).dividerColor, - iconColor: Colors.white, - onChanged: (bool? value) { - if (value == null) { - return; - } - - item.value = value; - widget.onChanged(index, value); - setState(() {}); - }, + Observer( + builder: (_) => StandardCheckbox( + value: item.value, + gradientBackground: true, + borderColor: Theme.of(context).dividerColor, + iconColor: Colors.white, + onChanged: (value) { + item.value = value; + onChanged(index, value); + }, + ), ), SizedBox(width: 16), - widget.displayItem?.call(item) ?? - Text( - item.title, - style: TextStyle( - fontSize: 14, - fontFamily: 'Lato', - fontWeight: FontWeight.w600, - color: item.isDisabled - ? Colors.grey.withOpacity(0.5) - : Theme.of(context).extension()!.titleColor, - decoration: TextDecoration.none, + Expanded( + child: displayItem?.call(item) ?? + Text( + item.title, + style: TextStyle( + fontSize: 14, + fontFamily: 'Lato', + fontWeight: FontWeight.w600, + color: item.isDisabled + ? Colors.grey.withOpacity(0.5) + : Theme.of(context).extension()!.titleColor, + decoration: TextDecoration.none, + ), ), - ) + ) ], ), ), ); } } - -class CheckBoxItem { - CheckBoxItem(this.title, this.value, {this.isDisabled = false}); - - final String title; - final bool isDisabled; - bool value; -} diff --git a/lib/store/check_box_picker_store.dart b/lib/store/check_box_picker_store.dart new file mode 100644 index 000000000..faa90965e --- /dev/null +++ b/lib/store/check_box_picker_store.dart @@ -0,0 +1,15 @@ +import 'package:mobx/mobx.dart'; + +part 'check_box_picker_store.g.dart'; + +class CheckBoxItem = _CheckBoxItem with _$CheckBoxItem; + +abstract class _CheckBoxItem with Store { + _CheckBoxItem(this.title, this.value, {this.isDisabled = false}); + + final String title; + final bool isDisabled; + + @observable + bool value; +} \ No newline at end of file