Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/herald/app_design.dart';
5 : import 'package:app_finance/_classes/herald/app_locale.dart';
6 : import 'package:app_finance/_configs/theme_helper.dart';
7 : import 'package:app_finance/design/form/list_selector.dart';
8 : import 'package:app_finance/design/form/list_selector_item.dart';
9 : import 'package:app_finance/design/wrapper/row_widget.dart';
10 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
11 : import 'package:dart_intl_search/dart_intl_search.dart';
12 : import 'package:material_ui/material_ui.dart';
13 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
14 :
15 : class BaseListSelectorItem extends ListSelectorItem {
16 : Currency item;
17 :
18 0 : @override
19 0 : Widget build(BuildContext context) => LayoutBuilder(builder: (_, constraints) {
20 0 : return Padding(
21 : padding: const EdgeInsets.all(2),
22 0 : child: RowWidget(
23 : chunk: const [32, null],
24 : indent: 0.0,
25 0 : maxWidth: constraints.maxWidth - ThemeHelper.getIndent(),
26 0 : alignment: AppDesign.getAlignment<MainAxisAlignment>(),
27 0 : children: [
28 0 : [
29 0 : item.flag != null ? Text(item.flag!) : ThemeHelper.emptyBox,
30 : ],
31 0 : [
32 0 : Align(
33 0 : alignment: AppDesign.isRightToLeft() ? Alignment.centerRight : Alignment.centerLeft,
34 0 : child: TextWrapper('${item.symbol} | ${item.name} (${item.code})'),
35 : ),
36 : ],
37 : ],
38 : ),
39 : );
40 : });
41 :
42 0 : void pinItem(BuildContext context, item) {
43 0 : final isPinned = CurrencyProvider.isPinned(item);
44 : if (isPinned) {
45 0 : CurrencyProvider.unpin(item);
46 : } else {
47 0 : CurrencyProvider.pin(item);
48 : }
49 0 : ScaffoldMessenger.of(context).showSnackBar(
50 0 : SnackBar(content: Text(isPinned ? AppLocale.labels.orderUnpin : AppLocale.labels.orderPin)),
51 : );
52 : }
53 :
54 0 : @override
55 0 : Widget suggest(BuildContext context) => Row(
56 0 : mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(),
57 0 : children: [
58 0 : SizedBox(
59 : width: 42,
60 0 : child: item.flag != null ? Text(item.flag!) : null,
61 : ),
62 0 : Expanded(
63 0 : child: Column(
64 0 : children: [
65 0 : Align(
66 0 : alignment: AppDesign.isRightToLeft() ? Alignment.topRight : Alignment.topLeft,
67 0 : child: TextWrapper(item.code),
68 : ),
69 0 : Align(
70 0 : alignment: AppDesign.isRightToLeft() ? Alignment.topRight : Alignment.topLeft,
71 0 : child: TextWrapper(item.name),
72 : ),
73 : ],
74 : ),
75 : ),
76 0 : SizedBox(
77 : width: 32,
78 0 : child: Center(child: Text(item.symbol)),
79 : ),
80 0 : SizedBox(
81 : width: 12,
82 0 : child: InkWell(
83 0 : onTap: () => pinItem(context, item),
84 0 : child: Icon(
85 0 : CurrencyProvider.isPinned(item) ? Icons.star : Icons.star_border,
86 0 : color: Theme.of(context).colorScheme.primary.withValues(alpha: 0.5),
87 : ),
88 : ),
89 : ),
90 : ],
91 : );
92 :
93 0 : @override
94 : bool match(String search) =>
95 0 : search.isPartOf('$item', Locale(AppLocale.code)) || '$item'.toLowerCase().contains(search.toLowerCase());
96 :
97 2 : @override
98 6 : bool equal(val) => val is String ? item.code == val : item.code == (val as BaseListSelectorItem).id;
99 :
100 2 : @override
101 2 : String get id => item.code;
102 :
103 0 : @override
104 0 : String get name => item.name;
105 :
106 4 : BaseListSelectorItem(this.item) : super(id: '', name: '');
107 : }
108 :
109 : class BaseCurrencySelector extends ListSelector<BaseListSelectorItem> {
110 0 : BaseCurrencySelector({
111 : super.key,
112 : required super.setState,
113 : super.value,
114 : super.withLabel,
115 : super.hintColor,
116 : super.hintStyle,
117 : super.hintText,
118 : super.tooltip,
119 0 : }) : super(options: CurrencyProvider.getAll().map((e) => BaseListSelectorItem(e)).toList());
120 : }
|