175 lines
6.5 KiB
Dart
175 lines
6.5 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>();
|
|
final TextEditingController searchController = TextEditingController();
|
|
bool showNewOwner = false;
|
|
String? firstName;
|
|
String? lastName;
|
|
String? contact;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
searchController.text = (widget.viewModel.currentOwner == null)
|
|
? ""
|
|
: "${widget.viewModel.currentOwner!.firstName} ${widget.viewModel.currentOwner!.lastName}";
|
|
final theme = Theme.of(context);
|
|
return ListenableBuilder(
|
|
listenable: widget.viewModel,
|
|
builder: (context, child) => AlertDialog(
|
|
title: Center(child: Text("Propriétaire du livre")),
|
|
content: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
SizedBox(height: 5),
|
|
(showNewOwner || widget.viewModel.owners!.isEmpty)
|
|
? SizedBox()
|
|
: DropdownMenu<Owner>(
|
|
enableFilter: true,
|
|
controller: searchController,
|
|
label: Text("Rechercher un·e propriétaire"),
|
|
requestFocusOnTap: true,
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
onSelected: (Owner? owner) {
|
|
widget.viewModel.currentOwner = owner;
|
|
},
|
|
enableSearch: true,
|
|
),
|
|
(!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;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
showNewOwner = !showNewOwner;
|
|
});
|
|
},
|
|
child: Text((showNewOwner) ? "Annuler" : "Ajouter un propriétaire"),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
if (showNewOwner) {
|
|
if (_formKey.currentState!.validate()) {
|
|
_formKey.currentState!.save();
|
|
debugPrint("\n\n\n\n(1) SENDING REQUEST\n\n\n\n");
|
|
await widget.viewModel.addOwner(
|
|
firstName!,
|
|
lastName!,
|
|
contact!,
|
|
);
|
|
setState(() {
|
|
showNewOwner = false;
|
|
});
|
|
}
|
|
}
|
|
widget.onPressAccept(context);
|
|
},
|
|
child: Text(
|
|
(!showNewOwner && searchController.text == "")
|
|
? "Annuler"
|
|
: "Valider",
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|