You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
car_common_app/lib/widgets/dropdown/dropdow_field.dart

155 lines
5.0 KiB
Dart

import 'package:flutter/cupertino.dart';
3 years ago
import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/consts.dart';
1 year ago
import 'package:mc_common_app/extensions/int_extensions.dart';
3 years ago
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
3 years ago
class DropValue {
int id;
String value;
String subValue;
bool? isEnabled;
3 years ago
DropValue(this.id, this.value, this.subValue, {this.isEnabled = true});
3 years ago
bool operator ==(o) => o is DropValue && o.value == value && o.id == id;
@override
String toString() {
return 'DropValue{id: $id, value: $value, subValue: $subValue, isEnabled: $isEnabled}';
}
3 years ago
}
class DropdownField extends StatefulWidget {
final String? hint;
final String errorValue;
final List<DropValue>? list;
final DropValue? dropdownValue;
final Function(DropValue) onSelect;
final Function()? onTap;
final bool showAppointmentPickerVariant;
final TextStyle? textStyle;
2 years ago
final bool isSelectAble;
3 years ago
1 year ago
const DropdownField(this.onSelect,
{super.key, this.hint, this.list, this.dropdownValue, this.onTap, this.errorValue = "", this.showAppointmentPickerVariant = false, this.textStyle, this.isSelectAble = true});
3 years ago
@override
State<DropdownField> createState() => _DropdownFieldState();
}
class _DropdownFieldState extends State<DropdownField> {
DropValue? dropdownValue;
List<DropValue> defaultV = [
DropValue(1, "One", ""),
DropValue(2, "Two", ""),
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
dropdownValue = widget.dropdownValue;
return Column(
children: [
1 day ago
// Always show hint as label when provided
1 year ago
if (widget.hint != null) ...[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
(widget.hint ?? "").toText(
color: borderColor,
fontSize: 13,
fontWeight: MyFonts.Medium,
),
],
),
4.height,
],
1 day ago
// Show disabled text field when not selectable
if (!widget.isSelectAble)
Container(
decoration: Utils.containerColorRadiusBorderWidth(
MyColors.greyShadowColor,
0,
MyColors.greyACColor,
2
),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
width: double.infinity,
child: (dropdownValue?.value ?? "").toText(
color: MyColors.darkTextColor,
fontSize: 15,
fontWeight: MyFonts.Medium,
),
)
else
// Show normal dropdown when selectable
Container(
decoration: widget.showAppointmentPickerVariant
? null
: Utils.containerColorRadiusBorderWidth(
MyColors.white,
0,
MyColors.darkPrimaryColor,
2
),
margin: const EdgeInsets.all(0),
padding: const EdgeInsets.only(left: 8, right: 8),
width: widget.showAppointmentPickerVariant ? 170 : null,
child: DropdownButton<DropValue>(
value: dropdownValue,
1 day ago
icon: const Icon(
Icons.keyboard_arrow_down_sharp,
size: 21,
),
elevation: 16,
iconSize: widget.showAppointmentPickerVariant ? 26 : 16,
iconEnabledColor: borderColor,
iconDisabledColor: borderColor,
isExpanded: true,
style: const TextStyle(
color: borderColor,
fontSize: 15,
),
underline: Container(height: 0),
onChanged: (DropValue? newValue) {
setState(() {
dropdownValue = newValue!;
widget.onSelect(newValue);
});
},
onTap: widget.onTap,
items: (widget.list ?? defaultV).map<DropdownMenuItem<DropValue>>(
1 day ago
(DropValue value) {
return DropdownMenuItem<DropValue>(
value: value,
enabled: value.isEnabled ?? true,
1 day ago
child: value.value.toText(
fontSize: 15,
color: value.isEnabled == false ? MyColors.darkTextColor : null,
fontWeight: MyFonts.Medium
),
);
},
).toList(),
),
),
if (widget.errorValue != "")
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
widget.errorValue.toText(fontSize: 14, color: Colors.red),
],
).paddingOnly(right: 10),
],
3 years ago
);
}
}