make checkbox stateless

Serhii committed Dec 12, 2022 at 00:07 UTC b536973077b27058126af54f930d1c3cf93f5013
4 files changed +11 -39
lib/src/screens/dashboard/widgets/filter_widget.dart
+1 -1
@@ -103,7 +103,7 @@ class FilterWidget extends StatelessWidget {
103 gradientBackground: true,
104 borderColor: Theme.of(context).dividerColor,
105 iconColor: Colors.white,
106 - onChanged: (bool val){},
106 + onChanged: (value) => item.onChanged(),
107 ))
108 : GestureDetector(
109 onTap: () async {
lib/src/screens/exchange/exchange_page.dart
-8
@@ -40,7 +40,6 @@ class ExchangePage extends BasePage {
40 final ExchangeViewModel exchangeViewModel;
41 final depositKey = GlobalKey<ExchangeCardState>();
42 final receiveKey = GlobalKey<ExchangeCardState>();
43 - final checkBoxKey = GlobalKey<StandardCheckboxState>();
43 final _formKey = GlobalKey<FormState>();
44 final _depositAmountFocus = FocusNode();
45 final _depositAddressFocus = FocusNode();
@@ -339,7 +338,6 @@ class ExchangePage extends BasePage {
338 mainAxisAlignment: MainAxisAlignment.start,
339 children: [
340 StandardCheckbox(
342 - key: checkBoxKey,
341 value: exchangeViewModel.isFixedRateMode,
342 caption: S.of(context).fixed_rate,
343 onChanged: (value) =>
@@ -682,12 +680,6 @@ class ExchangePage extends BasePage {
680 }
681 });
682
685 - reaction((_) => exchangeViewModel.isFixedRateMode, (bool value) {
686 - if (checkBoxKey.currentState!.value != exchangeViewModel.isFixedRateMode) {
687 - checkBoxKey.currentState!.value = exchangeViewModel.isFixedRateMode;
688 - }
689 - });
690 -
683 depositAddressController.addListener(
684 () => exchangeViewModel.depositAddress = depositAddressController.text);
685
lib/src/widgets/standard_checkbox.dart
+7 -27
@@ -2,16 +2,14 @@ import 'dart:ui';
2 import 'package:flutter/cupertino.dart';
3 import 'package:flutter/material.dart';
4
5 -class StandardCheckbox extends StatefulWidget {
5 +class StandardCheckbox extends StatelessWidget {
6 StandardCheckbox({
7 - Key? key,
7 required this.value,
8 this.caption = '',
9 this.gradientBackground = false,
10 this.borderColor,
11 this.iconColor,
13 - required this.onChanged})
14 - : super(key: key);
12 + required this.onChanged});
13
14 final bool value;
15 final String caption;
@@ -20,21 +18,7 @@ class StandardCheckbox extends StatefulWidget {
18 final Color? iconColor;
19 final Function(bool) onChanged;
20
23 - @override
24 - StandardCheckboxState createState() =>
25 - StandardCheckboxState(value, caption, onChanged);
26 -}
27 -
28 -class StandardCheckboxState extends State<StandardCheckbox> {
29 - StandardCheckboxState(this.value, this.caption, this.onChanged);
21
31 - bool value;
32 - String caption;
33 - Function(bool) onChanged;
34 -
35 - void changeValue(bool newValue) {
36 - setState(() => value = newValue);
37 - }
22
23 @override
24 Widget build(BuildContext context) {
@@ -45,7 +29,7 @@ class StandardCheckboxState extends State<StandardCheckbox> {
29 ], begin: Alignment.centerLeft, end: Alignment.centerRight);
30
31 final boxBorder = Border.all(
48 - color: widget.borderColor ?? Theme.of(context)
32 + color: borderColor ?? Theme.of(context)
33 .primaryTextTheme
34 .caption!
35 .color!,
@@ -53,8 +37,8 @@ class StandardCheckboxState extends State<StandardCheckbox> {
37
38
39 final checkedBoxDecoration = BoxDecoration(
56 - gradient: widget.gradientBackground ? baseGradient : null,
57 - border: widget.gradientBackground ? null : boxBorder,
40 + gradient: gradientBackground ? baseGradient : null,
41 + border: gradientBackground ? null : boxBorder,
42 borderRadius: BorderRadius.all(
43 Radius.circular(8.0)));
44
@@ -64,11 +48,7 @@ class StandardCheckboxState extends State<StandardCheckbox> {
48 Radius.circular(8.0)));
49
50 return GestureDetector(
67 - onTap: () {
68 - value = !value;
69 - onChanged(value);
70 - setState(() {});
71 - },
51 + onTap: () => onChanged(!value),
52 child: Row(
53 mainAxisSize: MainAxisSize.min,
54 mainAxisAlignment: MainAxisAlignment.start,
@@ -80,7 +60,7 @@ class StandardCheckboxState extends State<StandardCheckbox> {
60 child: value
61 ? Icon(
62 Icons.check,
83 - color: widget.iconColor ?? Colors.blue,
63 + color: iconColor ?? Colors.blue,
64 size: 20.0,
65 )
66 : Offstage(),
lib/view_model/dashboard/dashboard_view_model.dart
+3 -3
@@ -61,15 +61,15 @@ abstract class DashboardViewModelBase with Store {
61 FilterItem(
62 value: transactionFilterStore.displayAll,
63 caption: S.current.all_transactions,
64 - onChanged: () => transactionFilterStore.toggleIAll()),
64 + onChanged: transactionFilterStore.toggleIAll),
65 FilterItem(
66 value: transactionFilterStore.displayIncoming,
67 caption: S.current.incoming,
68 - onChanged: () => transactionFilterStore.toggleIncoming()),
68 + onChanged:transactionFilterStore.toggleIncoming),
69 FilterItem(
70 value: transactionFilterStore.displayOutgoing,
71 caption: S.current.outgoing,
72 - onChanged: () => transactionFilterStore.toggleOutgoing()),
72 + onChanged: transactionFilterStore.toggleOutgoing),
73 // FilterItem(
74 // value: () => false,
75 // caption: S.current.transactions_by_date,