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/_classes/structure/navigation/app_route.dart';
6 : import 'package:app_finance/_configs/theme_helper.dart';
7 : import 'package:material_ui/material_ui.dart';
8 :
9 : typedef FnListWidget = Widget Function(dynamic item, BuildContext context);
10 :
11 : class BaseListLimitedWidget extends StatelessWidget {
12 : final String? route;
13 : final String routeList;
14 : final dynamic state;
15 : final int? limit;
16 : final double width;
17 : final FnListWidget buildListWidget;
18 :
19 6 : const BaseListLimitedWidget({
20 : super.key,
21 : required this.route,
22 : required this.state,
23 : required this.limit,
24 : required this.routeList,
25 : required this.width,
26 : required this.buildListWidget,
27 : });
28 :
29 6 : Widget buildButton(BuildContext context, String route, String title) {
30 6 : NavigatorState nav = Navigator.of(context);
31 6 : return TextButton(
32 0 : onPressed: () {
33 0 : nav.pushNamed(AppRoute.homeRoute);
34 0 : nav.pushNamed(route);
35 : },
36 6 : child: Text(title),
37 : );
38 : }
39 :
40 6 : @override
41 : Widget build(BuildContext context) {
42 18 : int itemCount = state.length + 2;
43 : bool hasMore = false;
44 30 : if (limit != null && limit! < state.length) {
45 0 : itemCount = limit! + 2;
46 : hasMore = true;
47 : }
48 : final addButton =
49 36 : route == null ? ThemeHelper.emptyBox : buildButton(context, '${route!}/add', AppLocale.labels.btnAdd);
50 :
51 6 : return ListView.builder(
52 : scrollDirection: Axis.vertical,
53 : shrinkWrap: true,
54 : itemCount: itemCount,
55 6 : itemBuilder: (context, index) {
56 6 : if (index == 0) {
57 : return ThemeHelper.hIndent;
58 12 : } else if (index <= itemCount - 2) {
59 6 : final item = state[index - 1];
60 4 : return buildListWidget(item, context);
61 0 : } else if (hasMore && limit != null && limit! > 0) {
62 0 : return Row(
63 : mainAxisAlignment: MainAxisAlignment.spaceBetween,
64 0 : children: [
65 0 : if (route != null) buildButton(context, route!, AppLocale.labels.btnMore),
66 0 : addButton,
67 : ],
68 : );
69 18 : } else if (limit != null && limit! > 0) {
70 : return addButton;
71 : }
72 : return ThemeHelper.emptyBox;
73 : });
74 : }
75 : }
|