Here I have four Lists as in the code below and is it possible to call the list through parameter od any someother way for reusable purpose, example if I have four DropDown Button and each DropDown Button contains different Lists so how i can create a reusable code for implement this.
Here is what i tried,
import 'package:flutter/material.dart';
class CustomDropDown extends StatefulWidget {
final String HintName;
final List<String> MDPT;
CustomDropDown({required this.HintName, required this.MDPT});
@override
_CustomDropDownState createState() => _CustomDropDownState();
}
class _CustomDropDownState extends State<CustomDropDown> {
String? dropdownvalue;
var MDPackageType = [
'Package Type 1',
'Package Type 2',
'Package Type 3',
'Package Type 4',
];
var MDPackageName = [
'Package Name 1',
'Package Name 2',
'Package Name 3',
'Package Name 4',
];
var PTPackageType = [
'Package Type 1',
'Package Type 2',
'Package Type 3',
'Package Type 4',
];
var PTPackageName = [
'Package Type 1',
'Package Type 2',
'Package Type 3',
'Package Type 4',
];
@override
Widget build(BuildContext context) {
return FormField<String>(
builder: (FormFieldState<String> state) {
return InputDecorator(
decoration: InputDecoration(
// labelStyle: textStyle,
// errorStyle: TextStyle(color: Colors.redAccent, fontSize: 16.0),
hintText: 'Package Type',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
isEmpty: dropdownvalue == '',
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: Text(
widget.HintName,
style: TextStyle(color: Colors.blue),
),
value: dropdownvalue,
isDense: true,
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue;
state.didChange(newValue);
});
},
items: MDPT.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value,
style: TextStyle(color: Colors.blue),
),
);
}).toList(),
),
),
);
},
);
}
Use the following function to build
DropdownMenuItem
list using aList
.Use
items: buildDropdownMenuItems(MDPT)
property to call the function.So the full code as below.