You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class NewTransaction extends StatefulWidget {
final Function addTransactionCallback;
const NewTransaction(this.addTransactionCallback, {super.key});
@override
State<NewTransaction> createState() => _NewTransactionState();
}
class _NewTransactionState extends State<NewTransaction> {
final _titleController = TextEditingController();
final _amourtController = TextEditingController();
DateTime? _selectedDate;
void _showDatePicker() {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now().subtract(Duration(days: 7)),
lastDate: DateTime.now())
.then((date) {
if (date == null) {
return;
}
setState(() {
_selectedDate = date;
});
});
}
void _sumbitData() {
final enteredTitle = _titleController.text;
final enteredAmount = double.parse(_amourtController.text);
if (enteredTitle.isEmpty || enteredAmount <= 0 || _selectedDate == null) {
return;
}
widget.addTransactionCallback(enteredTitle, enteredAmount, _selectedDate);
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Card(
elevation: 5,
child: Container(
padding: EdgeInsets.only(
top: 10,
left: 10,
right: 10,
bottom: MediaQuery.of(context).viewInsets.bottom + 20,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextField(
decoration: const InputDecoration(labelText: 'Titre'),
controller: _titleController,
onSubmitted: (_) => _sumbitData(),
),
TextField(
decoration: const InputDecoration(labelText: 'Montant'),
controller: _amourtController,
keyboardType: TextInputType.number,
onSubmitted: (_) => _sumbitData(),
),
Container(
height: 70,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
_selectedDate == null
? 'Pas de date sélectionnée'
: DateFormat.yMd()
.format((_selectedDate as DateTime)),
style: TextStyle(fontWeight: FontWeight.bold),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple[300]),
onPressed: _showDatePicker,
child: Text(
_selectedDate == null
? 'Sélectionner une date'
: 'Sélectionner une autre date',
)),
],
),
),
ElevatedButton(
onPressed: _sumbitData, child: const Text('Ajouter'))
],
),
),
),
);
}
}