refactoring for recipient addition page
This commit is contained in:
121
frontend/pweb/lib/pages/payment_methods/add/ledger.dart
Normal file
121
frontend/pweb/lib/pages/payment_methods/add/ledger.dart
Normal file
@@ -0,0 +1,121 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pshared/models/payment/methods/ledger.dart';
|
||||
|
||||
import 'package:pweb/utils/text_field_styles.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
|
||||
class LedgerForm extends StatefulWidget {
|
||||
final void Function(LedgerPaymentMethod) onChanged;
|
||||
final LedgerPaymentMethod? initialData;
|
||||
final bool isEditable;
|
||||
|
||||
const LedgerForm({
|
||||
super.key,
|
||||
required this.onChanged,
|
||||
this.initialData,
|
||||
required this.isEditable,
|
||||
});
|
||||
|
||||
@override
|
||||
State<LedgerForm> createState() => _LedgerFormState();
|
||||
}
|
||||
|
||||
class _LedgerFormState extends State<LedgerForm> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
late final TextEditingController _ledgerAccountRefController;
|
||||
late final TextEditingController _contraLedgerAccountRefController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_ledgerAccountRefController = TextEditingController(
|
||||
text: widget.initialData?.ledgerAccountRef ?? '',
|
||||
);
|
||||
_contraLedgerAccountRefController = TextEditingController(
|
||||
text: widget.initialData?.contraLedgerAccountRef ?? '',
|
||||
);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _emitIfValid());
|
||||
}
|
||||
|
||||
void _emitIfValid() {
|
||||
if (_formKey.currentState?.validate() ?? false) {
|
||||
final contraRef = _contraLedgerAccountRefController.text.trim();
|
||||
widget.onChanged(
|
||||
LedgerPaymentMethod(
|
||||
ledgerAccountRef: _ledgerAccountRefController.text.trim(),
|
||||
contraLedgerAccountRef: contraRef.isEmpty ? null : contraRef,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant LedgerForm oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final newData = widget.initialData;
|
||||
final oldData = oldWidget.initialData;
|
||||
|
||||
if (newData == null && oldData != null) {
|
||||
_ledgerAccountRefController.clear();
|
||||
_contraLedgerAccountRefController.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (newData != null && newData != oldData) {
|
||||
final hasLedgerRefChange = newData.ledgerAccountRef != _ledgerAccountRefController.text;
|
||||
final hasContraRefChange = (newData.contraLedgerAccountRef ?? '') != _contraLedgerAccountRefController.text;
|
||||
|
||||
if (hasLedgerRefChange) _ledgerAccountRefController.text = newData.ledgerAccountRef;
|
||||
if (hasContraRefChange) {
|
||||
_contraLedgerAccountRefController.text = newData.contraLedgerAccountRef ?? '';
|
||||
}
|
||||
|
||||
if (hasLedgerRefChange || hasContraRefChange) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _emitIfValid());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
|
||||
return Form(
|
||||
key: _formKey,
|
||||
onChanged: _emitIfValid,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextFormField(
|
||||
readOnly: !widget.isEditable,
|
||||
controller: _ledgerAccountRefController,
|
||||
decoration: getInputDecoration(context, l10n.ledgerAccountRef, widget.isEditable),
|
||||
style: getTextFieldStyle(context, widget.isEditable),
|
||||
validator: (val) => (val == null || val.trim().isEmpty)
|
||||
? l10n.enterLedgerAccountRef
|
||||
: null,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
readOnly: !widget.isEditable,
|
||||
controller: _contraLedgerAccountRefController,
|
||||
decoration: getInputDecoration(context, l10n.contraLedgerAccountRef, widget.isEditable),
|
||||
style: getTextFieldStyle(context, widget.isEditable),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ledgerAccountRefController.dispose();
|
||||
_contraLedgerAccountRefController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,15 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
class PaymentMethodTypeSelector extends StatelessWidget {
|
||||
final PaymentType? value;
|
||||
final List<PaymentType> types;
|
||||
final Set<PaymentType> disabledTypes;
|
||||
final ValueChanged<PaymentType?> onChanged;
|
||||
|
||||
const PaymentMethodTypeSelector({
|
||||
super.key,
|
||||
required this.value,
|
||||
required this.types,
|
||||
this.disabledTypes = const {},
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@@ -24,12 +28,18 @@ class PaymentMethodTypeSelector extends StatelessWidget {
|
||||
return DropdownButtonFormField<PaymentType>(
|
||||
initialValue: value,
|
||||
decoration: InputDecoration(labelText: l10n.paymentType),
|
||||
items: PaymentType.values.map((type) {
|
||||
items: types.map((type) {
|
||||
final label = getPaymentTypeLabel(context, type);
|
||||
return DropdownMenuItem(value: type, child: Text(label));
|
||||
final isDisabled = disabledTypes.contains(type);
|
||||
final effectiveLabel = isDisabled ? '$label - ${l10n.paymentMethodComingSoon}' : label;
|
||||
return DropdownMenuItem(
|
||||
value: type,
|
||||
enabled: !isDisabled,
|
||||
child: Text(effectiveLabel),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: onChanged,
|
||||
validator: (val) => val == null ? l10n.selectPaymentType : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:pshared/models/payment/type.dart';
|
||||
|
||||
import 'package:pweb/pages/payment_methods/add/method_selector.dart';
|
||||
import 'package:pweb/pages/payment_methods/form.dart';
|
||||
import 'package:pweb/utils/payment/availability.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
@@ -46,6 +47,8 @@ class _AddPaymentMethodDialogState extends State<AddPaymentMethodDialog> {
|
||||
children: [
|
||||
PaymentMethodTypeSelector(
|
||||
value: _selectedType,
|
||||
types: visiblePaymentTypes,
|
||||
disabledTypes: disabledPaymentTypes,
|
||||
onChanged: (val) => setState(() {
|
||||
_selectedType = val;
|
||||
_currentMethod = null;
|
||||
@@ -73,4 +76,4 @@ class _AddPaymentMethodDialogState extends State<AddPaymentMethodDialog> {
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user