From b536973077b27058126af54f930d1c3cf93f5013 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 12 Dec 2022 00:07:05 +0200 Subject: [PATCH] make checkbox stateless --- .../dashboard/widgets/filter_widget.dart | 2 +- lib/src/screens/exchange/exchange_page.dart | 8 ----- lib/src/widgets/standard_checkbox.dart | 34 ++++--------------- .../dashboard/dashboard_view_model.dart | 6 ++-- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/lib/src/screens/dashboard/widgets/filter_widget.dart b/lib/src/screens/dashboard/widgets/filter_widget.dart index a3172940f..411006633 100644 --- a/lib/src/screens/dashboard/widgets/filter_widget.dart +++ b/lib/src/screens/dashboard/widgets/filter_widget.dart @@ -103,7 +103,7 @@ class FilterWidget extends StatelessWidget { gradientBackground: true, borderColor: Theme.of(context).dividerColor, iconColor: Colors.white, - onChanged: (bool val){}, + onChanged: (value) => item.onChanged(), )) : GestureDetector( onTap: () async { diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index 744848156..7b73a134b 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -40,7 +40,6 @@ class ExchangePage extends BasePage { final ExchangeViewModel exchangeViewModel; final depositKey = GlobalKey(); final receiveKey = GlobalKey(); - final checkBoxKey = GlobalKey(); final _formKey = GlobalKey(); final _depositAmountFocus = FocusNode(); final _depositAddressFocus = FocusNode(); @@ -339,7 +338,6 @@ class ExchangePage extends BasePage { mainAxisAlignment: MainAxisAlignment.start, children: [ StandardCheckbox( - key: checkBoxKey, value: exchangeViewModel.isFixedRateMode, caption: S.of(context).fixed_rate, onChanged: (value) => @@ -682,12 +680,6 @@ class ExchangePage extends BasePage { } }); - reaction((_) => exchangeViewModel.isFixedRateMode, (bool value) { - if (checkBoxKey.currentState!.value != exchangeViewModel.isFixedRateMode) { - checkBoxKey.currentState!.value = exchangeViewModel.isFixedRateMode; - } - }); - depositAddressController.addListener( () => exchangeViewModel.depositAddress = depositAddressController.text); diff --git a/lib/src/widgets/standard_checkbox.dart b/lib/src/widgets/standard_checkbox.dart index 9202ea7f5..d7b947a7e 100644 --- a/lib/src/widgets/standard_checkbox.dart +++ b/lib/src/widgets/standard_checkbox.dart @@ -2,16 +2,14 @@ import 'dart:ui'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -class StandardCheckbox extends StatefulWidget { +class StandardCheckbox extends StatelessWidget { StandardCheckbox({ - Key? key, required this.value, this.caption = '', this.gradientBackground = false, this.borderColor, this.iconColor, - required this.onChanged}) - : super(key: key); + required this.onChanged}); final bool value; final String caption; @@ -20,21 +18,7 @@ class StandardCheckbox extends StatefulWidget { final Color? iconColor; final Function(bool) onChanged; - @override - StandardCheckboxState createState() => - StandardCheckboxState(value, caption, onChanged); -} -class StandardCheckboxState extends State { - StandardCheckboxState(this.value, this.caption, this.onChanged); - - bool value; - String caption; - Function(bool) onChanged; - - void changeValue(bool newValue) { - setState(() => value = newValue); - } @override Widget build(BuildContext context) { @@ -45,7 +29,7 @@ class StandardCheckboxState extends State { ], begin: Alignment.centerLeft, end: Alignment.centerRight); final boxBorder = Border.all( - color: widget.borderColor ?? Theme.of(context) + color: borderColor ?? Theme.of(context) .primaryTextTheme .caption! .color!, @@ -53,8 +37,8 @@ class StandardCheckboxState extends State { final checkedBoxDecoration = BoxDecoration( - gradient: widget.gradientBackground ? baseGradient : null, - border: widget.gradientBackground ? null : boxBorder, + gradient: gradientBackground ? baseGradient : null, + border: gradientBackground ? null : boxBorder, borderRadius: BorderRadius.all( Radius.circular(8.0))); @@ -64,11 +48,7 @@ class StandardCheckboxState extends State { Radius.circular(8.0))); return GestureDetector( - onTap: () { - value = !value; - onChanged(value); - setState(() {}); - }, + onTap: () => onChanged(!value), child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, @@ -80,7 +60,7 @@ class StandardCheckboxState extends State { child: value ? Icon( Icons.check, - color: widget.iconColor ?? Colors.blue, + color: iconColor ?? Colors.blue, size: 20.0, ) : Offstage(), diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 40ea2d78b..ac38a43cc 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -61,15 +61,15 @@ abstract class DashboardViewModelBase with Store { FilterItem( value: transactionFilterStore.displayAll, caption: S.current.all_transactions, - onChanged: () => transactionFilterStore.toggleIAll()), + onChanged: transactionFilterStore.toggleIAll), FilterItem( value: transactionFilterStore.displayIncoming, caption: S.current.incoming, - onChanged: () => transactionFilterStore.toggleIncoming()), + onChanged:transactionFilterStore.toggleIncoming), FilterItem( value: transactionFilterStore.displayOutgoing, caption: S.current.outgoing, - onChanged: () => transactionFilterStore.toggleOutgoing()), + onChanged: transactionFilterStore.toggleOutgoing), // FilterItem( // value: () => false, // caption: S.current.transactions_by_date,