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/design_type.dart';
7 : import 'package:app_finance/design/form/list_selector_item.dart';
8 : import 'package:app_finance/_configs/theme_helper.dart';
9 : import 'package:app_finance/_ext/build_context_ext.dart';
10 : import 'package:app_finance/_ext/color_ext.dart';
11 : import 'package:app_finance/design/form/simple_input.dart';
12 : import 'package:material_ui/material_ui.dart';
13 : import 'package:flutter/services.dart';
14 : import 'package:flutter_grid_layout/flutter_grid_layout.dart';
15 : import 'package:provider/provider.dart';
16 :
17 : typedef FntSelectorCallback = Widget Function(
18 : List<ListSelectorItem> options,
19 : NavigatorState nav,
20 : );
21 :
22 : class ListSelectorPage<T extends Object?> extends StatefulWidget {
23 : final T? result;
24 : final List<ListSelectorItem> options;
25 : final String tooltip;
26 : final FntSelectorCallback? itemBuilder;
27 : final void Function(T?)? onTap;
28 :
29 1 : const ListSelectorPage({
30 : super.key,
31 : required this.options,
32 : required this.tooltip,
33 : this.result,
34 : this.itemBuilder,
35 : this.onTap,
36 : });
37 :
38 1 : @override
39 1 : State<StatefulWidget> createState() => ListSelectorPageState<T>();
40 : }
41 :
42 : class ListSelectorPageState<T extends Object?> extends State<ListSelectorPage<T>> {
43 : final controller = TextEditingController();
44 : final focus = FocusNode();
45 : Animation<double>? _animation;
46 : late NavigatorState nav;
47 : dynamic result;
48 : List<ListSelectorItem> options = [];
49 : String value = '';
50 :
51 1 : @override
52 : void initState() {
53 3 : result = widget.result;
54 3 : options = widget.options;
55 10 : controller.addListener(() => controller.text.isEmpty && value.isEmpty ? () {} : setState(filter));
56 1 : super.initState();
57 4 : WidgetsBinding.instance.addPostFrameCallback((_) => _waitForTransition());
58 : }
59 :
60 1 : @override
61 : void dispose() {
62 3 : _animation?.removeStatusListener(_onTransition);
63 2 : controller.dispose();
64 2 : focus.dispose();
65 1 : super.dispose();
66 : }
67 :
68 1 : void _waitForTransition() {
69 1 : if (!mounted) {
70 : return;
71 : }
72 4 : _animation = ModalRoute.of(context)?.animation;
73 3 : if (_animation == null || _animation!.isCompleted) {
74 0 : _showKeyboard();
75 : } else {
76 3 : _animation!.addStatusListener(_onTransition);
77 : }
78 : }
79 :
80 1 : void _onTransition(AnimationStatus status) {
81 1 : if (status.isCompleted) {
82 3 : _animation?.removeStatusListener(_onTransition);
83 1 : _showKeyboard();
84 : }
85 : }
86 :
87 1 : void _showKeyboard() {
88 1 : if (!mounted) {
89 : return;
90 : }
91 2 : if (focus.hasFocus) {
92 1 : SystemChannels.textInput.invokeMethod('TextInput.show');
93 : } else {
94 0 : focus.requestFocus();
95 : }
96 : }
97 :
98 1 : void filter() {
99 3 : value = controller.text;
100 8 : options = widget.options.where((e) => e.match(value)).toList();
101 : }
102 :
103 1 : void onTap(T? item) {
104 2 : if (widget.onTap != null) {
105 0 : widget.onTap!(item);
106 : } else {
107 2 : nav.pop<T>(item);
108 : }
109 : }
110 :
111 1 : Widget itemBuilder(List<ListSelectorItem> options) =>
112 2 : widget.itemBuilder?.call(options, nav) ??
113 1 : ListView.builder(
114 1 : itemCount: options.length,
115 1 : itemBuilder: (BuildContext context, int index) {
116 1 : return ListTile(
117 5 : tileColor: index % 2 == 0 ? context.colorScheme.primary.withValues(alpha: 0.05) : null,
118 3 : hoverColor: context.colorScheme.primary.withValues(alpha: 0.15),
119 2 : title: options[index].suggest(context),
120 3 : onTap: () => onTap(options[index] as T),
121 : );
122 : },
123 : );
124 :
125 1 : @override
126 : Widget build(BuildContext context) {
127 1 : final design = Provider.of<AppDesign>(context, listen: false);
128 2 : if (design.value == AppDesignType.germany) {
129 0 : options.sort((a, b) => a.name.compareTo(b.name));
130 : }
131 1 : final indent = ThemeHelper.getIndent();
132 2 : nav = Navigator.of(context);
133 1 : return Directionality(
134 : textDirection: TextDirection.ltr,
135 1 : child: Scaffold(
136 3 : appBar: AppBar(backgroundColor: context.colorScheme.primary, toolbarHeight: 0),
137 1 : body: Column(
138 1 : crossAxisAlignment: AppDesign.getAlignment(),
139 1 : mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(),
140 1 : children: [
141 1 : Container(
142 2 : padding: EdgeInsets.all(ThemeHelper.getIndent()),
143 : width: double.infinity,
144 1 : decoration: BoxDecoration(
145 2 : color: context.colorScheme.surface,
146 1 : boxShadow: [
147 1 : BoxShadow(
148 3 : color: context.colorScheme.onSurface.withValues(alpha: 0.1),
149 : offset: const Offset(0, 1),
150 : blurRadius: 3,
151 : spreadRadius: 1,
152 : ),
153 1 : BoxShadow(
154 2 : color: context.colorScheme.surface,
155 : offset: const Offset(0, 3),
156 : blurRadius: 0,
157 : spreadRadius: 0,
158 : ),
159 : ],
160 5 : border: Border(bottom: BorderSide(width: 4, color: context.colorScheme.surface.withValues(alpha: 0.2))),
161 : ),
162 1 : child: Container(
163 1 : decoration: BoxDecoration(
164 2 : color: context.colorScheme.surface,
165 4 : border: Border.all(color: context.colorScheme.onSurface.withValues(alpha: 0.1)),
166 3 : borderRadius: BorderRadius.all(Radius.circular(indent / 2)),
167 : ),
168 1 : height: ThemeHelper.barHeight + indent,
169 : width: double.infinity,
170 2 : padding: EdgeInsets.all(indent / 2),
171 1 : child: GridContainer(
172 1 : alignment: AppDesign.getAlignment<MainAxisAlignment>(),
173 3 : rows: [null, ThemeHelper.barHeight / 2, ThemeHelper.barHeight / 2, ThemeHelper.barHeight, indent],
174 : // ignore: prefer_const_literals_to_create_immutables
175 1 : columns: [ThemeHelper.barHeight],
176 1 : children: [
177 1 : GridItem(
178 : start: const Size(0, 0),
179 : end: const Size(2, 1),
180 1 : child: SimpleInput(
181 1 : controller: controller,
182 1 : focusNode: focus,
183 2 : tooltip: widget.tooltip,
184 : withLabel: true,
185 : forceFocus: true,
186 0 : onFieldSubmitted: (String value) =>
187 0 : onTap(widget.options.where((e) => e.match(value)).firstOrNull as T?),
188 : ),
189 : ),
190 1 : GridItem(
191 : start: const Size(1, 0),
192 : end: const Size(3, 1),
193 1 : child: IconButton(
194 2 : tooltip: AppLocale.labels.clear,
195 1 : style: ButtonStyle(
196 1 : backgroundColor: WidgetStateProperty.all<Color>(
197 6 : context.colorScheme.surface.mesh(context.colorScheme.primary.withValues(alpha: 1), 0.1),
198 : ),
199 : ),
200 : icon: const Icon(Icons.clear),
201 0 : onPressed: () => onTap(null),
202 : ),
203 : ),
204 1 : GridItem(
205 : start: const Size(3, 0),
206 : end: const Size(4, 1),
207 1 : child: IconButton(
208 2 : tooltip: AppLocale.labels.returnBack,
209 : icon: const Icon(Icons.rotate_left_rounded),
210 0 : onPressed: () => onTap(result as T?),
211 : ),
212 : ),
213 : ],
214 : ),
215 : ),
216 : ),
217 1 : if (result != null)
218 1 : ListTile(
219 3 : tileColor: context.colorScheme.primary.withValues(alpha: 0.15),
220 3 : hoverColor: context.colorScheme.primary.withValues(alpha: 0.20),
221 8 : title: widget.options.where((e) => e.equal(result)).firstOrNull?.build(context) ?? ThemeHelper.emptyBox,
222 0 : onTap: () => onTap(result as T?),
223 : ),
224 5 : Expanded(child: Padding(padding: EdgeInsets.all(indent), child: itemBuilder(options))),
225 : ],
226 : ),
227 : ),
228 : );
229 : }
230 : }
|