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 createState() => _OwnerPopupState(); } class _OwnerPopupState extends State { final GlobalKey _formKey = GlobalKey(); 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() : SingleChildScrollView( padding: EdgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom, ), child: DropdownMenu( 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( theme.highlightColor, ) : WidgetStatePropertyAll( theme.canvasColor, ), ), ), ], onSelected: (Owner? owner) { widget.viewModel.currentOwner = owner; }, enableSearch: true, ), ), (!showNewOwner) ? SizedBox() : Form( key: _formKey, child: Column( children: [ TextFormField( decoration: InputDecoration( labelText: "Prénom", border: OutlineInputBorder(), ), onSaved: (newValue) { setState(() { firstName = newValue?.trim(); }); }, validator: (value) { if (value == null || value.isEmpty) { return "Indiquez un prénom"; } return null; }, keyboardType: TextInputType.name, ), SizedBox(height: 10), TextFormField( decoration: InputDecoration( labelText: "Nom", border: OutlineInputBorder(), ), onSaved: (newValue) { setState(() { lastName = newValue?.trim(); }); }, validator: (value) { if (value == null || value.isEmpty) { return "Indiquez un nom"; } return null; }, keyboardType: TextInputType.name, ), SizedBox(height: 10), TextFormField( decoration: InputDecoration( labelText: "Contact", border: OutlineInputBorder(), ), onSaved: (newValue) { setState(() { contact = newValue?.trim(); }); }, 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(); await widget.viewModel.addOwner( firstName!, lastName!, contact!, ); setState(() { showNewOwner = false; }); } } widget.onPressAccept(context); }, child: Text( (!showNewOwner && searchController.text == "") ? "Annuler" : "Valider", ), ), ], ), ); } }