import 'package:counters/counters.dart'; import 'package:counters/datbase.dart'; import 'package:counters/value.dart'; import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:intl/intl.dart'; class NewValuePage extends StatelessWidget { NewValuePage({super.key, required this.counter}); factory NewValuePage.forCounter({required Counter counter}) { switch (counter.counterType) { case CounterType.coldWater: return NewValuePageWithOneRate(counter: counter); case CounterType.hotWater: return NewValuePageWithOneRate(counter: counter); case CounterType.gas: return NewValuePageWithOneRate(counter: counter); case CounterType.electrisity1rates: return NewValuePageWithOneRate(counter: counter); case CounterType.electrisity2rates: return NewValuePageWithTwoRates(counter: counter); case CounterType.electrisity3rates: return NewValuePageWithThreeRates(counter: counter); } } final Counter counter; final dataFormat = DateFormat("dd-MM-yyyy"); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(AppLocalizations.of(context)!.new_value_title)), body: SizedBox.shrink(), ); } } class NewValuePageWithOneRate extends NewValuePage { final valueController = TextEditingController(); final dateController = TextEditingController(); NewValuePageWithOneRate({super.key, required super.counter}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(AppLocalizations.of(context)!.new_value_title)), body: Padding( padding: const EdgeInsets.all(8.0), child: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ DateTimeField( format: dataFormat, initialValue: DateTime.now(), controller: dateController, onShowPicker: (context, currentValue) async { final date = await showDatePicker( context: context, initialDate: currentValue ?? DateTime.now(), firstDate: DateTime(2000), lastDate: DateTime(2100), ); return date; }, ), const SizedBox(height: 20), TextField( controller: valueController, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'(^-?\d*\.?\d*)')) ], decoration: InputDecoration( label: Text(AppLocalizations.of(context)!.new_value_title), //hintStyle: const TextStyle(color: Colors.blue), hintText: '0.0', suffixText: counter.counterType.getUnits().getLabel(context))), const SizedBox(height: 50), TextButton( onPressed: () { DBProvider.db .newValue(Value( counterId: counter.id!, date: dataFormat.parse(dateController.text).toUtc(), rate1Id: 0, value1: double.tryParse(valueController.text) ?? 0.0, value2: 0, value3: 0)) .then((value) => Navigator.pop(context)); }, child: Text( AppLocalizations.of(context)!.add_new_address_button)) ], ), ), ), ); } } class NewValuePageWithTwoRates extends NewValuePage { final value1Controller = TextEditingController(); final value2Controller = TextEditingController(); final dateController = TextEditingController(); NewValuePageWithTwoRates({super.key, required super.counter}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(AppLocalizations.of(context)!.new_value_title)), body: Padding( padding: const EdgeInsets.all(8.0), child: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ DateTimeField( format: DateFormat("dd-MM-yyyy"), initialValue: DateTime.now(), controller: dateController, onShowPicker: (context, currentValue) async { final date = await showDatePicker( context: context, initialDate: currentValue ?? DateTime.now(), firstDate: DateTime(2000), lastDate: DateTime(2100), ); return date; }, ), const SizedBox(height: 20), TextField( controller: value1Controller, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'(^-?\d*\.?\d*)')) ], decoration: InputDecoration( label: Text(AppLocalizations.of(context)!.new_value_title), //hintStyle: const TextStyle(color: Colors.blue), hintText: '0.0', suffixText: counter.counterType.getUnits().getLabel(context))), TextField( controller: value2Controller, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'(^-?\d*\.?\d*)')) ], decoration: InputDecoration( label: Text(AppLocalizations.of(context)!.new_value_title), //hintStyle: const TextStyle(color: Colors.blue), hintText: '0.0', suffixText: counter.counterType.getUnits().getLabel(context))), const SizedBox(height: 50), TextButton( onPressed: () { DBProvider.db .newValue(Value( counterId: counter.id!, date: dataFormat.parse(dateController.text).toUtc(), rate1Id: 0, value1: double.tryParse(value1Controller.text) ?? 0.0, value2: double.tryParse(value2Controller.text) ?? 0.0, value3: 0)) .then((value) => Navigator.pop(context)); }, child: Text( AppLocalizations.of(context)!.add_new_address_button)) ], ), ), ), ); } } class NewValuePageWithThreeRates extends NewValuePage { NewValuePageWithThreeRates({super.key, required super.counter}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(AppLocalizations.of(context)!.new_value_title)), body: SizedBox.shrink(), ); } }