| 1 | import 'package:cake_wallet/src/widgets/list_row.dart'; |
| 2 | import 'package:cake_wallet/src/widgets/picker.dart'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | |
| 5 | class StandardPickerList<T> extends StatefulWidget { |
| 6 | StandardPickerList({ |
| 7 | Key? key, |
| 8 | required this.title, |
| 9 | required this.value, |
| 10 | required this.items, |
| 11 | required this.displayItem, |
| 12 | required this.onSliderChanged, |
| 13 | required this.onItemSelected, |
| 14 | required this.selectedIdx, |
| 15 | required this.customItemIndex, |
| 16 | required this.customValue, |
| 17 | this.maxValue, |
| 18 | }) : super(key: key); |
| 19 | |
| 20 | final String title; |
| 21 | final List<T> items; |
| 22 | final int customItemIndex; |
| 23 | final String Function(T item, double sliderValue) displayItem; |
| 24 | final Function(double) onSliderChanged; |
| 25 | final Function(T item, double sliderValue) onItemSelected; |
| 26 | final String value; |
| 27 | final int selectedIdx; |
| 28 | final double customValue; |
| 29 | final double? maxValue; |
| 30 | |
| 31 | @override |
| 32 | _StandardPickerListState<T> createState() => _StandardPickerListState<T>(); |
| 33 | } |
| 34 | |
| 35 | class _StandardPickerListState<T> extends State<StandardPickerList<T>> { |
| 36 | late String value; |
| 37 | late int selectedIdx; |
| 38 | late double customValue; |
| 39 | |
| 40 | @override |
| 41 | void initState() { |
| 42 | super.initState(); |
| 43 | |
| 44 | value = widget.value; |
| 45 | selectedIdx = widget.selectedIdx; |
| 46 | customValue = widget.customValue; |
| 47 | } |
| 48 | |
| 49 | @override |
| 50 | Widget build(BuildContext context) { |
| 51 | String adaptedDisplayItem(T item) => widget.displayItem(item, customValue); |
| 52 | String adaptedOnItemSelected(T item) => widget.onItemSelected(item, customValue).toString(); |
| 53 | |
| 54 | return Column( |
| 55 | children: [ |
| 56 | ListRow(title: '${widget.title}:', value: value), |
| 57 | Padding( |
| 58 | padding: const EdgeInsets.only(left: 24, right: 24, top: 0, bottom: 24), |
| 59 | child: Picker( |
| 60 | items: widget.items, |
| 61 | displayItem: adaptedDisplayItem, |
| 62 | selectedAtIndex: selectedIdx, |
| 63 | customItemIndex: widget.customItemIndex, |
| 64 | maxValue: widget.maxValue, |
| 65 | headerEnabled: false, |
| 66 | closeOnItemSelected: false, |
| 67 | mainAxisAlignment: MainAxisAlignment.center, |
| 68 | sliderValue: customValue, |
| 69 | isWrapped: false, |
| 70 | borderColor: Theme.of(context).colorScheme.outlineVariant, |
| 71 | onSliderChanged: (newValue) { |
| 72 | setState(() => customValue = newValue); |
| 73 | value = widget.onSliderChanged(newValue).toString(); |
| 74 | }, |
| 75 | onItemSelected: (T item) { |
| 76 | setState(() => selectedIdx = widget.items.indexOf(item)); |
| 77 | value = adaptedOnItemSelected(item); |
| 78 | }, |
| 79 | ), |
| 80 | ), |
| 81 | ], |
| 82 | ); |
| 83 | } |
| 84 | } |