| 1 | import 'package:cake_wallet/src/widgets/standard_checkbox.dart'; |
| 2 | import 'package:cake_wallet/utils/responsive_layout_util.dart'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | import 'package:cake_wallet/src/widgets/picker_wrapper_widget.dart'; |
| 5 | |
| 6 | class CheckBoxPicker extends StatefulWidget { |
| 7 | CheckBoxPicker({ |
| 8 | required this.items, |
| 9 | required this.onChanged, |
| 10 | required this.title, |
| 11 | this.displayItem, |
| 12 | this.isSeparated = true, |
| 13 | }); |
| 14 | |
| 15 | final List<CheckBoxItem> items; |
| 16 | final String title; |
| 17 | final Widget Function(CheckBoxItem)? displayItem; |
| 18 | final bool isSeparated; |
| 19 | final Function(int, bool) onChanged; |
| 20 | |
| 21 | @override |
| 22 | CheckBoxPickerState createState() => CheckBoxPickerState(items); |
| 23 | } |
| 24 | |
| 25 | class CheckBoxPickerState extends State<CheckBoxPicker> { |
| 26 | CheckBoxPickerState(this.items); |
| 27 | |
| 28 | final List<CheckBoxItem> items; |
| 29 | |
| 30 | ScrollController controller = ScrollController(); |
| 31 | |
| 32 | @override |
| 33 | Widget build(BuildContext context) { |
| 34 | return PickerWrapperWidget( |
| 35 | children: [ |
| 36 | if (widget.title.isNotEmpty) |
| 37 | Container( |
| 38 | padding: EdgeInsets.symmetric(horizontal: 24), |
| 39 | child: Text( |
| 40 | widget.title, |
| 41 | textAlign: TextAlign.center, |
| 42 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 43 | fontSize: 18, |
| 44 | fontWeight: FontWeight.bold, |
| 45 | decoration: TextDecoration.none, |
| 46 | color: Theme.of(context).colorScheme.onSurface, |
| 47 | ), |
| 48 | ), |
| 49 | ), |
| 50 | Padding( |
| 51 | padding: EdgeInsets.only(left: 24, right: 24, top: 24), |
| 52 | child: ClipRRect( |
| 53 | borderRadius: BorderRadius.all(Radius.circular(30)), |
| 54 | child: Container( |
| 55 | color: Theme.of(context).colorScheme.surfaceContainer, |
| 56 | child: ConstrainedBox( |
| 57 | constraints: BoxConstraints( |
| 58 | maxHeight: MediaQuery.of(context).size.height * 0.65, |
| 59 | maxWidth: ResponsiveLayoutUtilBase.kPopupWidth, |
| 60 | ), |
| 61 | child: Column( |
| 62 | mainAxisSize: MainAxisSize.min, |
| 63 | children: [ |
| 64 | Flexible( |
| 65 | child: Stack( |
| 66 | alignment: Alignment.center, |
| 67 | children: <Widget>[ |
| 68 | items.length > 3 |
| 69 | ? Scrollbar( |
| 70 | controller: controller, |
| 71 | child: itemsList(), |
| 72 | ) |
| 73 | : itemsList(), |
| 74 | ], |
| 75 | ), |
| 76 | ), |
| 77 | ], |
| 78 | ), |
| 79 | ), |
| 80 | ), |
| 81 | ), |
| 82 | ), |
| 83 | ], |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | Widget itemsList() { |
| 88 | return Container( |
| 89 | color: Theme.of(context).colorScheme.outlineVariant, |
| 90 | child: ListView.separated( |
| 91 | padding: EdgeInsets.zero, |
| 92 | controller: controller, |
| 93 | shrinkWrap: true, |
| 94 | separatorBuilder: (context, index) => widget.isSeparated |
| 95 | ? Divider( |
| 96 | color: Theme.of(context).colorScheme.outlineVariant, |
| 97 | height: 1, |
| 98 | ) |
| 99 | : const SizedBox(), |
| 100 | itemCount: items.isEmpty ? 0 : items.length, |
| 101 | itemBuilder: (context, index) => buildItem(index), |
| 102 | ), |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | Widget buildItem(int index) { |
| 107 | final item = items[index]; |
| 108 | |
| 109 | return GestureDetector( |
| 110 | onTap: () { |
| 111 | bool newValue = !item.value; |
| 112 | item.value = newValue; |
| 113 | widget.onChanged(index, newValue); |
| 114 | setState(() {}); |
| 115 | }, |
| 116 | child: Container( |
| 117 | height: 55, |
| 118 | color: Theme.of(context).colorScheme.surfaceContainer, |
| 119 | padding: EdgeInsets.only(left: 24, right: 24), |
| 120 | child: Row( |
| 121 | children: [ |
| 122 | StandardCheckbox( |
| 123 | value: item.value, |
| 124 | gradientBackground: true, |
| 125 | borderColor: Theme.of(context).colorScheme.outlineVariant, |
| 126 | iconColor: Theme.of(context).colorScheme.onPrimary, |
| 127 | onChanged: (bool? value) { |
| 128 | if (value == null) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | item.value = value; |
| 133 | widget.onChanged(index, value); |
| 134 | setState(() {}); |
| 135 | }, |
| 136 | ), |
| 137 | SizedBox(width: 16), |
| 138 | widget.displayItem?.call(item) ?? |
| 139 | Text( |
| 140 | item.title, |
| 141 | style: Theme.of(context).textTheme.bodyMedium?.copyWith( |
| 142 | fontWeight: FontWeight.w600, |
| 143 | color: item.isDisabled |
| 144 | ? Theme.of(context).colorScheme.onSurfaceVariant.withOpacity(0.5) |
| 145 | : Theme.of(context).colorScheme.onSurface, |
| 146 | decoration: TextDecoration.none, |
| 147 | ), |
| 148 | ) |
| 149 | ], |
| 150 | ), |
| 151 | ), |
| 152 | ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | class CheckBoxItem { |
| 157 | CheckBoxItem(this.title, this.value, {this.isDisabled = false}); |
| 158 | |
| 159 | final String title; |
| 160 | final bool isDisabled; |
| 161 | bool value; |
| 162 | } |