import 'package:flutter/material.dart'; import 'package:multi_select_flutter/multi_select_flutter.dart'; import 'package:manager_app/constants.dart'; class MultiSelectDropdownLanguageContainer extends StatelessWidget { final Color color; final String label; final String labelHint; final List values; final List initialValue; final bool isMultiple; final bool isAtLeastOne; final double fontSize; final ValueChanged> onChanged; const MultiSelectDropdownLanguageContainer({ Key? key, this.color = kSecond, required this.label, this.labelHint = "Veuillez sélectionner une langue", required this.values, required this.initialValue, required this.isMultiple, this.isAtLeastOne = false, this.fontSize = 18, required this.onChanged, }) : super(key: key); @override Widget build(BuildContext context) { return FormField>( initialValue: initialValue, builder: (state) { return InputDecorator( decoration: InputDecoration( labelText: label, border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)), contentPadding: EdgeInsets.symmetric(horizontal: 25, vertical: 20), ), child: Builder( builder: (context) { return MultiSelectDialogField( items: values.map((e) => MultiSelectItem(e, e)).toList(), listType: MultiSelectListType.LIST, initialValue: state.value ?? [], buttonText: Text( (state.value == null || state.value!.isEmpty) ? "Aucune sélection" : _buildSummary(state.value!), ), title: Text(labelHint), searchable: true, selectedColor: kPrimaryColor, checkColor: Colors.white, chipDisplay: MultiSelectChipDisplay.none(), dialogHeight: MediaQuery.of(context).size.height * 0.4, dialogWidth: MediaQuery.of(context).size.width * 0.6, onConfirm: (selected) { if (isAtLeastOne && selected.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Au moins une valeur doit être sélectionnée"), ), ); } else { onChanged(selected.cast()); state.didChange(selected); } }, ); }, ), ); }, ); } String _buildSummary(List selected) { if (selected.isEmpty) return "Aucune sélection"; if (selected.length <= 5) return selected.join(", "); return "${selected.length} sélectionnés"; } }