import 'package:flutter/material.dart'; import 'package:kmobile/data/models/user.dart'; class StopSingleChequeScreen extends StatefulWidget { final User selectedAccount; final String date; final String instrType; final String fromCheque; final String toCheque; const StopSingleChequeScreen( {super.key, required this.selectedAccount, required this.date, required this.instrType, required this.fromCheque, required this.toCheque}); @override State createState() => _StopSingleChequeScreenState(); } class _StopSingleChequeScreenState extends State { final _formKey = GlobalKey(); final _stopFromChequeNoController = TextEditingController(); final _stopIssueDateController = TextEditingController(); final _stopExpiryDateController = TextEditingController(); final _stopAmountController = TextEditingController(); final _stopCommentController = TextEditingController(); bool _validateChequeNumber() { final value = _stopFromChequeNoController.text; if (value.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Please enter a cheque number')), ); return false; } final chequeNumber = int.tryParse(value); final fromCheque = int.tryParse(widget.fromCheque); final toCheque = int.tryParse(widget.toCheque); if (chequeNumber == null || fromCheque == null || toCheque == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Invalid cheque number format')), ); return false; } if (chequeNumber < fromCheque || chequeNumber > toCheque) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( 'Cheque number must be between ${widget.fromCheque} and ${widget.toCheque}')), ); return false; } return true; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Stop Single Cheque'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: ListView( children: [ Card( elevation: 0, margin: const EdgeInsets.symmetric(vertical: 8.0), child: ListTile( leading: Image.asset( 'assets/images/logo.png', width: 40, height: 40, ), title: Text(widget.selectedAccount.accountNo!), subtitle: const Text("Account Number"), ), ), const SizedBox(height: 24), TextFormField( controller: _stopFromChequeNoController, decoration: const InputDecoration( labelText: 'Cheque Number', border: OutlineInputBorder(), ), keyboardType: TextInputType.number, validator: (value) { // This validator will only return null or empty string to allow SnackBar to display if (value == null || value.isEmpty) { return ''; // Return empty string to trigger error state without message } final chequeNumber = int.tryParse(value); final fromCheque = int.tryParse(widget.fromCheque); final toCheque = int.tryParse(widget.toCheque); if (chequeNumber == null || fromCheque == null || toCheque == null) { return ''; } if (chequeNumber < fromCheque || chequeNumber > toCheque) { return ''; } return null; }, ), const SizedBox(height: 16), TextFormField( initialValue: widget.instrType, readOnly: true, decoration: const InputDecoration( labelText: 'Instrument Type', border: OutlineInputBorder(), ), ), const SizedBox(height: 16), TextFormField( controller: _stopIssueDateController, decoration: const InputDecoration( labelText: 'Stop Issue Date', border: OutlineInputBorder(), ), keyboardType: TextInputType.datetime, ), const SizedBox(height: 16), TextFormField( controller: _stopExpiryDateController, decoration: const InputDecoration( labelText: 'Stop Expiry Date', border: OutlineInputBorder(), ), keyboardType: TextInputType.datetime, ), const SizedBox(height: 16), TextFormField( controller: _stopAmountController, decoration: const InputDecoration( labelText: 'Stop Amount', border: OutlineInputBorder(), ), keyboardType: TextInputType.number, ), const SizedBox(height: 16), TextFormField( controller: _stopCommentController, decoration: const InputDecoration( labelText: 'Stop Comment', border: OutlineInputBorder(), ), ), const SizedBox(height: 16), TextFormField( initialValue: widget.date, readOnly: true, decoration: const InputDecoration( labelText: 'Chequebook Issue Date', border: OutlineInputBorder(), ), ), const SizedBox(height: 32), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate() && _validateChequeNumber()) { // TODO: Implement stop single cheque logic } }, child: const Text('Stop Cheque'), ), ], ), ), ), ); } }