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:flutter/material.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 1 : 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 1 : Widget buildButton(BuildContext context, String route, String title) { 30 1 : NavigatorState nav = Navigator.of(context); 31 1 : return TextButton( 32 0 : onPressed: () { 33 0 : nav.pushNamed(AppRoute.homeRoute); 34 0 : nav.pushNamed(route); 35 : }, 36 1 : child: Text(title), 37 : ); 38 : } 39 : 40 1 : @override 41 : Widget build(BuildContext context) { 42 3 : int itemCount = state.length + 2; 43 : bool hasMore = false; 44 5 : if (limit != null && limit! < state.length) { 45 0 : itemCount = limit! + 2; 46 : hasMore = true; 47 : } 48 : final addButton = 49 6 : route == null ? ThemeHelper.emptyBox : buildButton(context, '${route!}/add', AppLocale.labels.btnAdd); 50 : 51 1 : return ListView.builder( 52 : scrollDirection: Axis.vertical, 53 : shrinkWrap: true, 54 : itemCount: itemCount, 55 1 : itemBuilder: (context, index) { 56 1 : if (index == 0) { 57 : return ThemeHelper.hIndent; 58 2 : } else if (index <= itemCount - 2) { 59 3 : final item = state[index - 1]; 60 2 : return buildListWidget(item, context); 61 : } else if (hasMore) { 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 : } else { 70 : return addButton; 71 : } 72 : }); 73 : } 74 : }