here is my code of dropDownButton.I'm trying to making select GenderList dropDownButton but when I assign variable selectedGender to value of dropDownButton it's hide hint text and default select first index value Male. I want this output
Here is my code when I comment on the value then it shows a hint text let's see my code below.
import'package:flutter/material.dart';
class DropDownButton extends StatefulWidget {
const DropDownButton({Key? key}) : super(key: key);
@override
State<DropDownButton> createState() => _DropDownButtonState();
}
class _DropDownButtonState extends State<DropDownButton> {
List<DropdownMenuItem<int>> genderList=[];
void loadGenderlist(){
genderList=[];
genderList.add(const DropdownMenuItem(
child: Text('Male'),
value: 0,));
genderList.add(const DropdownMenuItem(
child: Text('Female'),
value: 1,));
genderList.add(const DropdownMenuItem(
child: Text('Other'),
value: 2,));
}
String selectval = "United Kingdom";
int selectedGender = 0;
@override
Widget build(BuildContext context) {
loadGenderlist();
return Scaffold(
appBar: AppBar(
title: Text("Form Validation with All Controls"),
backgroundColor: Colors.redAccent
),
body: SafeArea(
child: ListView(
children: [
DecoratedBox(
decoration: BoxDecoration(
color: Colors.green,
border: Border.all(color: Colors.black38,width: 3)
),
child: DropdownButton(
hint: Text("Select Gender"),
items: genderList,
//value: selectedGender,
onChanged: (value){
setState(() {
selectedGender =int.parse(value.toString()) ;
});
},
underline: Container(),
),
)
],
),
)
);
}
}
I want this output: