113 lines
3.3 KiB
Dart
113 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
|
|
class SingleChoiceInputContainer<T> extends StatefulWidget {
|
|
final String label;
|
|
final String selectLabel;
|
|
final T? selected;
|
|
final List<T> values;
|
|
final ValueChanged<T?> onChanged;
|
|
final double borderRadius;
|
|
final Color selectedColor;
|
|
final Color textColor;
|
|
final String Function(T) valueExtractor;
|
|
final String Function(T) labelExtractor;
|
|
|
|
const SingleChoiceInputContainer({
|
|
Key? key,
|
|
required this.label,
|
|
required this.selectLabel,
|
|
required this.selected,
|
|
required this.values,
|
|
required this.onChanged,
|
|
required this.valueExtractor,
|
|
required this.labelExtractor,
|
|
this.borderRadius = 12,
|
|
this.selectedColor = kPrimaryColor,
|
|
this.textColor = kWhite,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<SingleChoiceInputContainer<T>> createState() =>
|
|
_SingleChoiceInputContainerState<T>();
|
|
}
|
|
|
|
class _SingleChoiceInputContainerState<T>
|
|
extends State<SingleChoiceInputContainer<T>> {
|
|
T? _selected;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_selected = widget.selected;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Protection contre les valeurs non présentes dans la liste
|
|
String? selectedValue;
|
|
if (_selected != null) {
|
|
final exists = widget.values
|
|
.any((v) => widget.valueExtractor(v) == widget.valueExtractor(_selected!));
|
|
selectedValue = exists ? widget.valueExtractor(_selected!) : null;
|
|
}
|
|
|
|
return FormField<T?>(
|
|
initialValue: _selected,
|
|
builder: (state) {
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
widget.label,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.w400,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
const SizedBox(height: 8, width: 10),
|
|
Container(
|
|
width: 225,
|
|
height: 60,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: DropdownButton<String>(
|
|
value: selectedValue,
|
|
hint: Text(widget.selectLabel,
|
|
style: TextStyle(color: Colors.grey.shade600)),
|
|
isExpanded: true,
|
|
items: widget.values.map((v) {
|
|
return DropdownMenuItem<String>(
|
|
value: widget.valueExtractor(v),
|
|
child: Text(
|
|
widget.labelExtractor(v),
|
|
style: const TextStyle(fontSize: 14),
|
|
),
|
|
);
|
|
}).toList(),
|
|
onChanged: widget.values.isEmpty
|
|
? null
|
|
: (id) {
|
|
T? selected = widget.values
|
|
.cast<T?>()
|
|
.firstWhere(
|
|
(v) => widget.valueExtractor(v!) == id,
|
|
orElse: () => null,
|
|
);
|
|
|
|
setState(() {
|
|
_selected = selected;
|
|
});
|
|
widget.onChanged(selected);
|
|
state.didChange(selected);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|