This repository has been archived on 2025-08-25. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Seshat/lib/ui/add_page/widgets/owner_popup.dart
2025-08-05 10:32:18 +02:00

172 lines
6.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
class OwnerPopup extends StatefulWidget {
const OwnerPopup({
super.key,
required this.viewModel,
required this.onPressAccept,
});
final AddViewModel viewModel;
final Function(BuildContext) onPressAccept;
@override
State<OwnerPopup> createState() => _OwnerPopupState();
}
class _OwnerPopupState extends State<OwnerPopup> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
bool showNewOwner = false;
String? firstName;
String? lastName;
String? contact;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return ListenableBuilder(
listenable: widget.viewModel,
builder: (context, child) => AlertDialog(
title: Center(child: Text("Propriétaire du livre")),
content: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: Text(
(widget.viewModel.currentOwner == null)
? "Choix actuel : aucun"
: "Choix actuel : ${widget.viewModel.currentOwner!.firstName} ${widget.viewModel.currentOwner!.lastName}",
),
),
SizedBox(height: 5),
(showNewOwner)
? SizedBox()
: DropdownMenu<Owner>(
enableFilter: true,
label: Text("Rechercher un·e propriétaire"),
dropdownMenuEntries: [
for (var owner in widget.viewModel.owners!)
DropdownMenuEntry(
value: owner,
label: "${owner.firstName} ${owner.lastName}",
style: ButtonStyle(
backgroundColor:
(widget.viewModel.currentOwner == owner)
? WidgetStatePropertyAll<Color>(
theme.highlightColor,
)
: WidgetStatePropertyAll<Color>(
theme.canvasColor,
),
),
),
],
initialSelection: widget.viewModel.currentOwner,
onSelected: (Owner? owner) {
widget.viewModel.currentOwner = owner;
},
),
SizedBox(height: 20),
TextButton(
onPressed: () {
setState(() {
showNewOwner = !showNewOwner;
});
},
child: Text(
(showNewOwner) ? "Annuler" : "Ajouter un propriétaire",
),
),
(!showNewOwner)
? SizedBox()
: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
labelText: "Nom",
border: OutlineInputBorder(),
),
onSaved: (newValue) {
setState(() {
lastName = newValue;
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un nom";
}
return null;
},
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
labelText: "Prénom",
border: OutlineInputBorder(),
),
onSaved: (newValue) {
setState(() {
firstName = newValue;
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un prénom";
}
return null;
},
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
labelText: "Contact",
border: OutlineInputBorder(),
),
onSaved: (newValue) {
setState(() {
contact = newValue;
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un moyen de contact";
}
return null;
},
),
SizedBox(height: 10),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
setState(() {
widget.viewModel.currentOwner = widget.viewModel
.addOwner(firstName!, lastName!, contact!);
showNewOwner = false;
});
}
},
child: Text("Créer"),
),
],
),
),
],
),
actions: [
TextButton(
onPressed: () {
widget.onPressAccept(context);
},
child: Text("Valider"),
),
],
),
);
}
}