import 'dart:ffi'; import 'package:counters/address.dart'; import 'package:counters/datbase.dart'; import 'package:counters/new_counter_page.dart'; import 'package:counters/record_action_pane.dart'; import 'package:counters/update_counter_page.dart'; import 'package:counters/values_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'counters.dart'; class CountersPage extends StatefulWidget { const CountersPage( {super.key, required this.title, required this.comments, required this.address}); final Address address; final String title; final String comments; @override State createState() => _CountersPageState(); } class _CountersPageState extends State { @override Widget build(BuildContext context) { final counters = DBProvider.db.getCountersOfAddress(widget.address); return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( widget.title, style: Theme.of(context).textTheme.bodyLarge, ), Text( widget.comments, style: Theme.of(context).textTheme.bodySmall, ), ], ), ), body: Center( child: FutureBuilder( future: counters, builder: (context, snapshot) { if (snapshot.hasData && snapshot.data!.isNotEmpty) { return CountersListView( counters: snapshot.data!, onUpdated: () { setState(() {}); }); } return Text(AppLocalizations.of(context)!.empty_counters_list); }, )), floatingActionButton: FloatingActionButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => NewCounterPage( addressId: widget.address.id!, ))).then((_) => setState(() {})); }, tooltip: AppLocalizations.of(context)!.add_new_counter_tooltip, child: const Icon(Icons.add), )); } } class CountersListView extends StatefulWidget { const CountersListView({ super.key, required this.counters, this.onUpdated, }); final List counters; final VoidCallback? onUpdated; @override State createState() => _CountersListViewState(); } class _CountersListViewState extends State { @override Widget build(BuildContext context) { return ListView.builder( itemCount: widget.counters.length, itemBuilder: (context, index) { return Slidable( key: const ValueKey(0), endActionPane: RecordActionPane(onEdit: () { Navigator.push( context, MaterialPageRoute( builder: (context) => UpdateCounterPage( counter: widget.counters[index], ))).then((_) => widget.onUpdated?.call()); }, onDelete: () { DBProvider.db.deleteCounter(widget.counters[index]).then((value) { widget.onUpdated?.call(); }); }).build(context), child: SizedBox( height: 60, child: TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ValuesPage( counter: widget.counters[index], ))).then((value) => widget.onUpdated?.call()); }, style: TextButton.styleFrom(shape: const BeveledRectangleBorder()), child: Row( children: [ Icon(widget.counters[index].counterType.icon, color: widget.counters[index].counterType.color), Padding( padding: const EdgeInsets.all(8.0), child: Align( alignment: Alignment.centerLeft, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( widget.counters[index].counterType .getLabel(context), style: Theme.of(context).textTheme.bodyLarge, ), Row( children: [ Text( widget.counters[index].name.isNotEmpty ? "${widget.counters[index].name} " : "", style: Theme.of(context).textTheme.bodySmall, ), Text( '${widget.counters[index].value} ', //style: Theme.of(context).textTheme.bodySmall, ), Text( widget.counters[index].counterType .getUnits() .getLabel(context), style: Theme.of(context).textTheme.bodySmall, ) ], ) ], ), ), ), ], ), ), ), ); }, ); } }