import 'package:counters/counters.dart'; import 'package:counters/datbase.dart'; import 'package:counters/new_value_page.dart'; import 'package:counters/value.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:intl/intl.dart'; class ValuesPage extends StatefulWidget { const ValuesPage({super.key, required this.counter}); final Counter counter; @override State createState() => _ValuesPageState(); } class _ValuesPageState extends State { @override Widget build(BuildContext context) { var values = DBProvider.db.getValuesOfCounter(widget.counter); return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(AppLocalizations.of(context)!.app_title)), body: Center( child: FutureBuilder( future: values, builder: (context, snapshot) { if (snapshot.hasData && snapshot.data!.isNotEmpty) { return ValuesListView( counter: widget.counter, values: snapshot.data!.reversed.toList()); } return Text(AppLocalizations.of(context)!.empty_values_list); }, ), ), floatingActionButton: FloatingActionButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => NewValuePage.forCounter(counter: widget.counter))) .then((_) => setState(() {})); }, tooltip: AppLocalizations.of(context)!.add_new_counter_tooltip, child: const Icon(Icons.add), )); } } class ValuesListView extends StatelessWidget { ValuesListView({ super.key, required this.values, required this.counter, }); final List values; final Counter counter; final dataFormat = DateFormat("dd-MM-yyyy"); @override Widget build(BuildContext context) { return ListView.builder( itemCount: values.length, itemBuilder: (context, index) { var units = counter.counterType.getUnits().getLabel(context); var valueWidgets = List.empty(growable: true); valueWidgets.add(Text("T1 - ${values[index].value1} $units")); var sum = values[index].value1; var value = values[index]; if (value.value2 != null && counter.counterType.id > CounterType.electrisity1rates.id) { valueWidgets.add(Text("T2 - ${value.value2} $units")); sum += value.value2!; } if (value.value3 != null && counter.counterType.id > CounterType.electrisity2rates.id) { valueWidgets.add(Text("T3 - ${value.value3} $units")); sum += value.value3!; } if (valueWidgets.length > 1) { valueWidgets.add(Text("Итого - $sum $units")); } return Padding( padding: const EdgeInsets.only(left: 8.0, right: 8.0), child: Card( child: Align( alignment: Alignment.centerLeft, child: Padding( padding: const EdgeInsets.all(8.0), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Text(dataFormat.format(value.date.toLocal())), Text(' ${AppLocalizations.of(context)!.values}:'), ], ), Column( crossAxisAlignment: CrossAxisAlignment.end, children: valueWidgets, ) ], ), ), )), ); }, ); } }