224 lines
10 KiB
Dart
224 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:intl/date_symbol_data_local.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:sendtrain/daos/media_items_dao.dart';
|
|
import 'package:sendtrain/daos/sessions_dao.dart';
|
|
import 'package:sendtrain/database/database.dart' hide ActivityAction;
|
|
import 'package:sendtrain/extensions/string_extensions.dart';
|
|
import 'package:sendtrain/widgets/session_view.dart';
|
|
|
|
class SessionCard extends StatefulWidget {
|
|
final int type;
|
|
final Session session;
|
|
const SessionCard({super.key, this.type = 0, required this.session});
|
|
|
|
@override
|
|
State<SessionCard> createState() => _SessionCardState();
|
|
}
|
|
|
|
class _SessionCardState extends State<SessionCard> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final int type = widget.type;
|
|
final Session session = widget.session;
|
|
|
|
initializeDateFormatting('en');
|
|
final DateFormat dateFormat = DateFormat('yyyy-MM-dd');
|
|
|
|
Color color = (session.status == SessionStatus.started)
|
|
? Theme.of(context).colorScheme.primaryContainer
|
|
: Theme.of(context).colorScheme.surfaceContainerLow;
|
|
|
|
return FutureBuilder<List<MediaItem>>(
|
|
future: MediaItemsDao(Provider.of<AppDatabase>(context))
|
|
.fromSession(session),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
List<MediaItem> mediaItems = snapshot.data!;
|
|
|
|
if (type == 0) {
|
|
return Card(
|
|
color: color,
|
|
margin: const EdgeInsets.fromLTRB(15, 15, 15, 0),
|
|
clipBehavior: Clip.hardEdge,
|
|
child: InkWell(
|
|
splashColor: Colors.deepPurple,
|
|
onTap: () => 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();
|
|
}),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: <Widget>[
|
|
ListTile(
|
|
contentPadding: EdgeInsets.only(left: 15),
|
|
leading: Padding(
|
|
padding: const EdgeInsets.fromLTRB(0, 8, 0, 0),
|
|
child: Container(
|
|
width: 60,
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(
|
|
fit: BoxFit.cover,
|
|
image:
|
|
findMediaByType(mediaItems, 'image')),
|
|
// color: Colors.blue,
|
|
borderRadius: BorderRadius.all(
|
|
Radius.elliptical(10, 10)),
|
|
),
|
|
)),
|
|
title: Text(maxLines: 1, session.title.toTitleCase()),
|
|
subtitle: Text(
|
|
maxLines: 1,
|
|
dateFormat.format(session.date as DateTime)),
|
|
trailing: IconButton(
|
|
padding: EdgeInsets.all(0),
|
|
alignment: Alignment.topCenter,
|
|
icon: Icon(Icons.close_rounded),
|
|
onPressed: () {
|
|
showAdaptiveDialog(
|
|
context: context,
|
|
builder: (BuildContext context) => AlertDialog(
|
|
title: const Text('Session Removal'),
|
|
content: const Text(
|
|
'Would you like to permanently remove this session?'),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
onPressed: () => {
|
|
Navigator.pop(context, 'Cancel'),
|
|
},
|
|
child: const Text('Cancel'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => {
|
|
SessionsDao(Provider.of<AppDatabase>(
|
|
context,
|
|
listen: false))
|
|
.remove(session)
|
|
.then((result) {
|
|
setState(() {});
|
|
}),
|
|
Navigator.pop(context, 'OK')
|
|
},
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
ListTile(
|
|
contentPadding:
|
|
const EdgeInsets.fromLTRB(15, 0, 15, 15),
|
|
title: Text(
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
style:
|
|
const TextStyle(fontWeight: FontWeight.w300),
|
|
session.content),
|
|
),
|
|
],
|
|
)),
|
|
);
|
|
} else {
|
|
return Card(
|
|
color: color,
|
|
child: InkWell(
|
|
// overlayColor: MaterialStateColor(Colors.deepPurple as int),
|
|
splashColor: Colors.deepPurple,
|
|
borderRadius:
|
|
const BorderRadius.all(Radius.elliptical(10, 10)),
|
|
onTap: () => 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();
|
|
}),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
// color: const Color.fromARGB(47, 0, 0, 0),
|
|
borderRadius: BorderRadius.circular(10),
|
|
image: DecorationImage(
|
|
colorFilter: ColorFilter.mode(
|
|
Color.fromARGB(220, 41, 39, 39),
|
|
BlendMode.hardLight),
|
|
image: findMediaByType(mediaItems, 'image'),
|
|
fit: BoxFit.cover),
|
|
),
|
|
child: Align(
|
|
alignment: Alignment.center,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: <Widget>[
|
|
ListTile(
|
|
title: Text(
|
|
maxLines: 3,
|
|
session.title.toTitleCase(),
|
|
textAlign: TextAlign.center),
|
|
subtitle: Text(
|
|
maxLines: 1,
|
|
dateFormat
|
|
.format(session.date as DateTime),
|
|
textAlign: TextAlign.center),
|
|
),
|
|
])))));
|
|
}
|
|
} else {
|
|
return Container(
|
|
alignment: Alignment.center,
|
|
child: SizedBox(
|
|
height: 50.0,
|
|
width: 50.0,
|
|
child: CircularProgressIndicator(),
|
|
));
|
|
}
|
|
});
|
|
}
|
|
|
|
ImageProvider findMediaByType(List<MediaItem> media, String type) {
|
|
Iterable<MediaItem>? found = media.where((m) => m.type == MediaType.image);
|
|
|
|
if (found.isNotEmpty) {
|
|
return NetworkImage(found.first.reference);
|
|
} else {
|
|
// Element is not found
|
|
return const AssetImage('assets/images/placeholder.jpg');
|
|
}
|
|
}
|
|
}
|