188 lines
9.6 KiB
Dart
188 lines
9.6 KiB
Dart
import 'package:drift/drift.dart' hide Column;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:sendtrain/daos/sessions_dao.dart';
|
|
import 'package:sendtrain/database/database.dart';
|
|
import 'package:sendtrain/widgets/session_view.dart';
|
|
|
|
class SessionCreator extends StatefulWidget {
|
|
const SessionCreator({super.key, this.data, this.session});
|
|
|
|
final Session? session;
|
|
final Map<String, dynamic>? data;
|
|
|
|
@override
|
|
State<SessionCreator> createState() => _SessionCreatorState();
|
|
}
|
|
|
|
class _SessionCreatorState extends State<SessionCreator> {
|
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
|
|
|
final Map<String, TextEditingController> sessionCreateController = {
|
|
'name': TextEditingController(),
|
|
'content': TextEditingController(),
|
|
'status': TextEditingController(),
|
|
'date': TextEditingController(),
|
|
};
|
|
|
|
Future createSession(context) {
|
|
return SessionsDao(Provider.of<AppDatabase>(context, listen: false))
|
|
.createOrUpdate(SessionsCompanion(
|
|
title: Value(sessionCreateController['name']!.text),
|
|
content: Value(sessionCreateController['content']!.text),
|
|
status: Value(SessionStatus.pending),
|
|
date:
|
|
Value(DateTime.parse(sessionCreateController['date']!.text))));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
sessionCreateController['date']!.text =
|
|
DateFormat('yyyy-MM-dd').format(DateTime.now());
|
|
|
|
return SimpleDialog(
|
|
title: Text('Create Session', textAlign: TextAlign.center),
|
|
titlePadding: EdgeInsets.only(top: 17.5, bottom: 10),
|
|
contentPadding: EdgeInsets.only(left: 30, right: 30, bottom: 15),
|
|
children: [
|
|
Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: EdgeInsets.only(top: 10, bottom: 10),
|
|
child: TextFormField(
|
|
controller: sessionCreateController['name'],
|
|
decoration: const InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
hintText: 'Enter session name',
|
|
),
|
|
validator: (String? value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter some text';
|
|
}
|
|
|
|
if (value.length <= 3 || value.length > 42) {
|
|
return 'Please enter between 3 and 42 characters';
|
|
}
|
|
return null;
|
|
},
|
|
)),
|
|
Padding(
|
|
padding: EdgeInsets.only(top: 10, bottom: 10),
|
|
child: TextFormField(
|
|
controller: sessionCreateController['content'],
|
|
decoration: const InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
hintText: 'Enter session description',
|
|
),
|
|
validator: (String? value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter some text';
|
|
}
|
|
|
|
if (value.length <= 3 || value.length > 256) {
|
|
return 'Please enter between 3 and 256 characters';
|
|
}
|
|
return null;
|
|
},
|
|
)),
|
|
Padding(
|
|
padding: EdgeInsets.only(top: 7.5, bottom: 7.5),
|
|
child: TextFormField(
|
|
readOnly: true,
|
|
decoration: const InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
hintText: 'Enter a date for the session',
|
|
),
|
|
controller: sessionCreateController['date'],
|
|
onTap: () {
|
|
showDatePicker(
|
|
context: context,
|
|
initialDate: DateTime.now(),
|
|
firstDate: DateTime.now()
|
|
.subtract(Duration(days: 365)),
|
|
lastDate:
|
|
DateTime.now().add(Duration(days: 365)))
|
|
.then((date) {
|
|
sessionCreateController['date']?.text =
|
|
DateFormat('yyyy-MM-dd').format(date!);
|
|
});
|
|
})),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
TextButton(
|
|
onPressed: () => {
|
|
if (_formKey.currentState!.validate())
|
|
{
|
|
createSession(_formKey.currentContext)
|
|
.then((id) => {
|
|
SessionsDao(
|
|
Provider.of<AppDatabase>(
|
|
context,
|
|
listen: false))
|
|
.find(id)
|
|
.then(
|
|
(session) =>
|
|
showGeneralDialog(
|
|
barrierColor: Colors
|
|
.black
|
|
.withOpacity(0.5),
|
|
transitionDuration:
|
|
const Duration(
|
|
milliseconds:
|
|
220),
|
|
transitionBuilder:
|
|
(BuildContext
|
|
context,
|
|
Animation<
|
|
double>
|
|
animation,
|
|
Animation<
|
|
double>
|
|
secondaryAnimation,
|
|
Widget
|
|
child) {
|
|
Animation<Offset> custom = Tween<
|
|
Offset>(
|
|
begin:
|
|
const Offset(
|
|
0.0,
|
|
1.0),
|
|
end: const Offset(
|
|
0.0,
|
|
0.0))
|
|
.animate(
|
|
animation);
|
|
return SlideTransition(
|
|
position:
|
|
custom,
|
|
child: Dialog.fullscreen(
|
|
child: SessionView(
|
|
session:
|
|
session)));
|
|
},
|
|
barrierDismissible:
|
|
true,
|
|
barrierLabel: '',
|
|
context: context,
|
|
pageBuilder: (context,
|
|
animation1,
|
|
animation2) {
|
|
return Container();
|
|
}).whenComplete(() => Navigator.pop(context, 'Submit')),
|
|
)
|
|
})
|
|
}
|
|
},
|
|
child: Text('Submit'))
|
|
])
|
|
],
|
|
))
|
|
]);
|
|
}
|
|
}
|