manager-app/lib/Components/multi_select_container.dart

138 lines
4.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:manager_app/constants.dart';
import 'package:html/parser.dart' show parse;
import 'message_notification.dart';
class MultiSelectContainer extends StatelessWidget {
final Color color;
final String? label;
final List<String> values;
final List<String> initialValue;
final bool isMultiple;
final bool isAtLeastOne;
final bool isHTMLLabel;
final double? width;
final int maxLines;
final ValueChanged<List<dynamic>> onChanged;
const MultiSelectContainer({
Key? key,
this.color = kSecond,
required this.label,
required this.values,
required this.initialValue,
required this.isMultiple,
this.isAtLeastOne = false,
this.isHTMLLabel = false,
this.width,
this.maxLines = 2,
required this.onChanged,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if(label != null)
Align(
alignment: AlignmentDirectional.centerStart,
child: Text(label!, style: TextStyle(fontSize: 25, fontWeight: FontWeight.w300))
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
width: width ?? size.width *0.25,
//color: Colors.yellow,
child: MultiSelectChip(
values,
initialValue,
isMultiple,
isAtLeastOne,
isHTMLLabel,
maxLines,
onSelectionChanged: (selectedList) {
onChanged(selectedList);
},
),
),
),
],
),
);
}
}
class MultiSelectChip extends StatefulWidget {
final List<String> values;
final List<String> selectedValues;
final Function(List<String>) onSelectionChanged; // +added
final bool isMultiple;
final bool isAtLeastOne;
final bool isHTMLLabel;
final int maxLines;
MultiSelectChip(
this.values,
this.selectedValues,
this.isMultiple,
this.isAtLeastOne,
this.isHTMLLabel,
this.maxLines,
{required this.onSelectionChanged} // +added
);
@override
_MultiSelectChipState createState() => _MultiSelectChipState();
}
class _MultiSelectChipState extends State<MultiSelectChip> {
int maxLines = 1; // Définir le nombre maximum de lignes
_buildChoiceList() {
List<Widget> choices = [];
widget.values.forEach((item) {
choices.add(Container(
padding: const EdgeInsets.all(2.0),
child: ChoiceChip(
label: Text(widget.isHTMLLabel ? parse(item).documentElement!.text : item, style: TextStyle(color: kBlack)),
selected: widget.selectedValues.contains(item),
selectedColor: kPrimaryColor,
onSelected: (selected) {
setState(() {
if (widget.isAtLeastOne && widget.selectedValues.length == 1 && widget.selectedValues[0] == item) {
showNotification(Colors.orange, kWhite, 'Au moins une valeur doit être sélectionnée', context, null);
} else {
if (widget.isMultiple) {
widget.selectedValues.contains(item)
? widget.selectedValues.remove(item)
: widget.selectedValues.add(item);
widget.onSelectionChanged(widget.selectedValues);
} else {
widget.selectedValues.clear();
widget.selectedValues.add(item);
widget.onSelectionChanged(widget.selectedValues);
}
}
});
},
),
));
});
return choices;
}
@override
Widget build(BuildContext context) {
return Container(
height: maxLines * 48.0, // Assuming each ChoiceChip is 48.0 height
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: _buildChoiceList(),
),
);
}
}