import 'package:flutter/material.dart';
import 'package:flutter_geeks/models/transaction.dart';
import 'package:provider/provider.dart';
import '../providers/transactions.dart';
class AddTransactionScreen extends StatefulWidget {
static const routeName = '/add-transaction';
const AddTransactionScreen({super.key});
@override
_AddTransactionScreenState createState() => _AddTransactionScreenState();
}
class _AddTransactionScreenState extends State<AddTransactionScreen> {
final _formKey = GlobalKey<FormState>();
String _category = 'Food';
double _amount = 0;
DateTime _selectedDate = DateTime.now();
String? _editId;
@override
void didChangeDependencies() {
super.didChangeDependencies();
final tx = ModalRoute.of(context)!.settings.arguments as Transaction?;
if (tx != null) {
setState(() {
_editId = tx.id;
_category = tx.category;
_amount = tx.amount;
_selectedDate = tx.date;
});
}
}
void _submitForm() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
if (_editId != null) {
Provider.of<TransactionsProvider>(
context,
listen: false,
).updateTransaction(_editId!, _category, _amount, _selectedDate);
} else {
Provider.of<TransactionsProvider>(
context,
listen: false,
).addTransaction(_category, _amount, _selectedDate);
}
Navigator.of(context).pop();
}
}
void _presentDatePicker() {
showDatePicker(
context: context,
initialDate: _selectedDate,
firstDate: DateTime(2020),
lastDate: DateTime.now(),
).then((pickedDate) {
if (pickedDate == null) {
return;
}
setState(() {
_selectedDate = pickedDate;
print(_selectedDate);
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue.shade800,
foregroundColor: Colors.white,
title: const Text('Add Transaction'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Amount',
),
keyboardType: TextInputType.number,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter an amount';
}
return null;
},
onSaved: (value) {
_amount = double.parse(value!);
},
initialValue: _amount.toString(),
),
SizedBox(height: 10),
DropdownButtonFormField(
decoration: InputDecoration(border: OutlineInputBorder()),
value: _category,
items:
['Food', 'Travel', 'Entertainment']
.map(
(label) => DropdownMenuItem(
value: label,
child: Text(label),
),
)
.toList(),
onChanged: (value) {
setState(() {
_category = value as String;
});
},
onSaved: (value) {
_category = value as String;
},
),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Date: ${_selectedDate.day.toString().padLeft(2, '0')}-'
'${_selectedDate.month.toString().padLeft(2, '0')}-'
'${_selectedDate.year}',
style: TextStyle(
fontSize: 21,
fontWeight: FontWeight.w500,
color: Colors.black,
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue.shade800,
foregroundColor: Colors.white,
),
onPressed: _presentDatePicker,
child: const Text('Choose Date'),
),
],
),
const SizedBox(height: 20),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue.shade800,
foregroundColor: Colors.white,
),
onPressed: _submitForm,
child: const Text('Add Transaction'),
),
],
),
),
),
);
}
}