import 'package:ambito/src/entity/measure/measure_repository.dart'; import 'package:ambito/src/widgets/form/fields/field_dropdown.dart'; import 'package:ambito/src/widgets/form/fields/field_monthsrangepicker.dart'; import 'package:ambito/src/widgets/form/form_widget.dart'; import 'package:ambito/src/widgets/form/form_widget_type.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:expandable_text/expandable_text.dart'; import 'package:flutter/material.dart'; import 'package:flutter_i18n/flutter_i18n.dart'; import '../../../main.dart'; import '../../entity/entities.dart'; class ActionsPage extends StatefulWidget { const ActionsPage({super.key}); @override State createState() => ActionsPageState(); } class ActionsPageState extends State { Map visible = {}; List effort = []; List effect = []; Set type = {}; String? filterType; Set areaType = {}; String? filterAreaType; List region = []; String? filterSupport; Set support = {}; String? filterMonths; Set months = {}; List massnahmen = []; @override void initState() { super.initState(); massnahmen = MeasureRepository().getAll(); effort = []; effect = []; type = {}; areaType = {}; region = []; support = {}; } buildCards() {} @override Widget build(BuildContext context) { final updatedTypes = {}; final updatedAreaTypes = {}; final updatedSupports = {}; final updatedMonths = {}; for (final massnahme in massnahmen) { if (massnahme.actionGroup?.value != null) { updatedTypes.add(massnahme.actionGroup!.value!); } massnahme.factsheetAreaType?.forEach((aType) { updatedAreaTypes.add(aType.value!); }); massnahme.fundingPrograms?.forEach((aType) { updatedSupports.add(aType.value!); }); massnahme.timeFrame?.forEach((tfType) { updatedMonths.add(tfType.value!); }); } setState(() { type.addAll(updatedTypes); areaType.addAll(updatedAreaTypes); support.addAll(updatedSupports); months.addAll(updatedMonths); }); final actionCards = massnahmen.map((massnahme) { final typeMatches = filterType == null || massnahme.actionGroup?.value == filterType; final areaTypeMatches = filterAreaType == null || (massnahme.factsheetAreaType ?.any((aType) => aType.value == filterAreaType) ?? false); final supportMatches = filterSupport == null || (massnahme.fundingPrograms ?.any((aType) => aType.value == filterSupport) ?? false); setState(() { visible[massnahme.id] = typeMatches && areaTypeMatches && supportMatches; }); return getCard(context, massnahme); }).toList(); return Padding( padding: const EdgeInsets.symmetric(horizontal: 32), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ getSpacer(), Center( child: SearchBar( leading: const Icon(Icons.search), onChanged: (value) { for (Measure massnahme in massnahmen) { if (!massnahme.name!.contains(value)) { logger.d('$value not in ${massnahme.name}'); setState(() { visible[massnahme.id] = false; }); } else { logger.d('$value in ${massnahme.name}'); setState(() { visible[massnahme.id] = true; }); } } }, ), ), getSpacer(), getSpacer(), Expanded( child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(width: 300, child: getFilter(context)), const SizedBox(width: 16), Expanded( child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: actionCards, ), ), ), ], ), ), getSpacer(), ], ), ); } Widget getSpacer() { return const SizedBox( height: 8, ); } Widget getFilter(BuildContext context) { List fields = []; fields.add( FieldDropdown( name: 'ort', label: 'Ort der Maßnahme', filterValue: filterAreaType, onClear: () { setState(() { filterAreaType = null; }); }, onSelected: ((String? value) { setState(() { filterAreaType = value; }); }), entries: areaType.toList(), ), ); fields.add( FieldDropdown( name: 'art', label: 'Art der Maßnahme', filterValue: filterType, onClear: () { setState(() { filterType = null; }); }, onSelected: ((String? value) { setState(() { filterType = value; }); }), entries: type.toList(), ), ); fields.add( FieldDropdown( name: 'support', label: 'Förderprogramm', filterValue: filterSupport, onClear: () { setState(() { filterSupport = null; }); }, onSelected: ((String? value) { setState(() { filterSupport = value; }); }), entries: support.toList(), ), ); List monthsSorted = []; for (int i = 1; i <= 12; i++) { monthsSorted .add(FlutterI18n.translate(context, '_general.lists.months.$i')); } fields.add( FieldMonthsRangepicker( name: 'months', label: 'Beginn der Maßnahme', filterValue: filterMonths, onClear: () {}, onSelected: (String? value) { logger.d(value); }, entries: monthsSorted, ), ); return FormWidget(type: FormWidgetType.vertical, fields: fields); } Widget getCard(BuildContext context, Measure massnahme) { final Map actionGroupColors = { 'Baulelemente': const Color(0xffFFD269).withOpacity(.4), 'Begrünung': const Color(0xff40DD74).withOpacity(.4), 'Bewirtschaftung': const Color(0xffBF72ED).withOpacity(.4), 'Nisthilfe': const Color(0xffDAE3FD).withOpacity(.4), 'Pflanzung': const Color(0xff40D6E9).withOpacity(.4), 'Sondermaßnahmen': const Color(0xff689EF1).withOpacity(.4), }; Color background = actionGroupColors[massnahme.actionGroup?.value] ?? Colors.white; CachedNetworkImage? image = massnahme.getThumbnail(); return Visibility( visible: visible[massnahme.id] ?? false, child: SizedBox( width: 800, child: Card( elevation: 2, color: Colors.white, child: ClipPath( clipper: ShapeBorderClipper( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: Container( decoration: BoxDecoration( border: Border( left: BorderSide( color: background, width: 15, ), ), ), child: Padding( padding: const EdgeInsets.all(32), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ (image != null) ? image : const SizedBox( width: 300, height: 140, ), const SizedBox( width: 16, ), Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( massnahme.name!, style: Theme.of(context).textTheme.headlineSmall, ), const SizedBox( height: 8, ), ExpandableText( style: Theme.of(context).textTheme.bodyLarge, massnahme.factsheetDefinition ?? '', maxLines: 4, expandText: 'mehr', collapseText: 'weniger', ) ], ), ) ], ), ), ), ), /*child: ,*/ ), ), ); } }