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_locale.dart';
5 : import 'package:app_finance/design/form/list_selector_item.dart';
6 : import 'package:app_finance/_configs/custom_text_theme.dart';
7 : import 'package:app_finance/_configs/theme_helper.dart';
8 : import 'package:app_finance/_ext/build_context_ext.dart';
9 : import 'package:app_finance/_ext/icons_ext.dart';
10 : import 'package:app_finance/design/form/list_selector.dart';
11 : import 'package:app_finance/design/form/list_selector_page.dart';
12 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
13 : import 'package:material_ui/material_ui.dart';
14 :
15 : class IconSelectorItem extends ListSelectorItem {
16 : IconData value;
17 :
18 0 : @override
19 0 : Widget build(context) => Icon(value, semanticLabel: name);
20 :
21 0 : @override
22 0 : Widget suggest(BuildContext context) => Column(
23 0 : children: [
24 : ThemeHelper.hIndent,
25 0 : Icon(value, semanticLabel: name, size: 32),
26 : ThemeHelper.hIndent,
27 0 : TextWrapper(name, style: context.textTheme.numberSmall),
28 : ],
29 : );
30 :
31 0 : @override
32 0 : bool equal(val) => value.toString() == val.toString();
33 :
34 0 : IconSelectorItem(this.value, {required super.name}) : super(id: value.toString());
35 : }
36 :
37 : class IconSelector extends ListSelector<IconSelectorItem> {
38 0 : IconSelector({
39 : super.key,
40 : required super.setState,
41 : super.value,
42 : super.withLabel,
43 : super.options = const [],
44 : String? hintText,
45 0 : }) : super(
46 0 : hintText: hintText ?? AppLocale.labels.iconTooltip,
47 0 : tooltip: AppLocale.labels.iconTooltip,
48 : );
49 :
50 0 : @override
51 0 : List<IconSelectorItem> get options => IconsExt.getAll().map((e) => IconSelectorItem(e.value, name: e.key)).toList();
52 :
53 0 : @override
54 0 : IconSelectorState createState() => IconSelectorState();
55 : }
56 :
57 : class IconSelectorState extends ListSelectorState<IconSelector, IconSelectorItem> {
58 : late int crossAxisCount;
59 :
60 0 : @override
61 : Widget build(BuildContext context) {
62 0 : crossAxisCount = ThemeHelper.getWidthCount(null, context) * 4;
63 0 : return super.build(context);
64 : }
65 :
66 0 : @override
67 0 : FntSelectorCallback getItemBuilder() => (List<ListSelectorItem> options, nav) => GridView.builder(
68 0 : gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
69 0 : crossAxisCount: crossAxisCount,
70 : ),
71 0 : itemCount: options.length,
72 : shrinkWrap: true,
73 0 : itemBuilder: (BuildContext context, int index) {
74 0 : bool oddRow = (index ~/ crossAxisCount) % 2 == 0;
75 0 : bool highlight = index % 2 == 0 && oddRow || index % 2 != 0 && !oddRow;
76 0 : return ListTile(
77 0 : tileColor: highlight ? context.colorScheme.primary.withValues(alpha: 0.05) : null,
78 0 : hoverColor: context.colorScheme.primary.withValues(alpha: 0.15),
79 0 : title: options[index].suggest(context),
80 0 : onTap: () => nav.pop<IconSelectorItem?>(options[index] as IconSelectorItem?),
81 : );
82 : },
83 : );
84 : }
|