Bottom overflow while having multiple line item in dropdown flutter - flutter

I have multiple line items in the dropdown menu in flutter like this :
It is shown perfectly fine in the dropdown pop up but in dropdown button it shows bottomoverflow like this :
Here is code for the same :
DropdownButtonHideUnderline(
child: DropdownButton(
items: addresses.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: SizedBox(
height: 10 * SizeConfig.heightMultiplier,
child: Column(
children: [
SizedBox(height: 1.5 * SizeConfig.heightMultiplier),
new Text(value, overflow: TextOverflow.clip),
SizedBox(height: 1.5 * SizeConfig.heightMultiplier),
],
),
),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
_selectedShippingAddress = newValue;
});
},
hint: Text("Select address"),
selectedItemBuilder: (BuildContext context) {
return addresses.map<Widget>((String item) {
return Container(
child: Text(item),
);
}).toList();
},
style: TextStyle(
fontSize: 1.9 *
SizeConfig.textMultiplier,
color:
Theme.of(context).accentColor,
fontFamily: 'Montserrat'),
value: _selectedShippingAddress,
isExpanded: true,
underline: Container(
height: 1,
color: Theme.of(context)
.textSelectionColor,
),
icon: Icon(Icons.arrow_drop_down),
isDense: true,
),
)
So what is a solution for this? Can anyone help on this?

From the example that you provided I replicated it and there is a problem in the blow code
DropdownMenuItem<String>(
value: value,
child: SizedBox(
height: 10 * SizeConfig.heightMultiplier,
Maybe the height is doing some problem just added a sample example below to let you know the things work.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SampleApp(),
debugShowCheckedModeBanner: false,
);
}
}
class SampleApp extends StatefulWidget {
#override
_SampleAppState createState() => _SampleAppState();
}
class _SampleAppState extends State<SampleApp> {
String _selectedShippingAddress;
List addresses = ['sample1', 'sample 2'];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your heading'),
),
body: DropdownButtonHideUnderline(
child: DropdownButton<String>(
items: addresses.map((value) {
return new DropdownMenuItem<String>(
value: value,
child: SizedBox(
child: Column(
children: [
SizedBox(height: 5),
new Text(value, overflow: TextOverflow.clip),
SizedBox(height: 5),
],
),
),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
_selectedShippingAddress = newValue;
});
},
hint: Text("Select address"),
selectedItemBuilder: (BuildContext context) {
return addresses.map<Widget>((item) {
return Container(
child: Text(item),
);
}).toList();
},
style: TextStyle(
fontSize: 14,
color: Theme.of(context).accentColor,
fontFamily: 'Montserrat'),
value: _selectedShippingAddress,
isExpanded: true,
underline: Container(
height: 1,
color: Theme.of(context).textSelectionColor,
),
icon: Icon(Icons.arrow_drop_down),
isDense: true,
),
));
}
}

Related

Dependent drop down that display data from real time database using flutter but I got error

In first drop down I display exam keys(spring and fall). In second drop semesters are shown according to user selected value of exam for example if user select fall from first drop down it display semester 1,3,5,7. Then if user select semester than relative courses are shown on another drop down
here is my database structure
I write code for this scenario. But it works only for first time if I change exam drop down value it gives me error
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
class homepg extends StatelessWidget {
const homepg({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("home"),
),
body: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var ref = FirebaseDatabase.instance.ref();
var _selectedexam;
var _selectedsemester;
var _selectedcourse;
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(15),
child: Center(
child: Column(
children: <Widget>[
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Exam ",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
),
const SizedBox(
width: 40,
),
Container(
width: 400,
child: FutureBuilder(
future: ref.child("/Exam").get(),
builder:
(context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key,
child: Text(snap.key.toString())))
.toList(),
value: _selectedexam,
onChanged: (String? newValue) {
setState(() {
_selectedexam = newValue!;
print(_selectedexam);
});
},
);
}),
),
],
),
const SizedBox(
height: 100,
),
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Semester",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
)),
const SizedBox(
width: 20,
),
Container(
width: 400,
child: FutureBuilder(
future: ref.child("/Exam/$_selectedexam").get(),
builder:
(context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key,
child: Text(snap.key.toString())))
.toList(),
value: _selectedsemester,
onChanged: (String? newValue) {
setState(() {
//_selectedexam = null;
_selectedsemester = newValue!;
print(_selectedsemester);
});
},
);
}),
),
],
),
const SizedBox(
height: 100,
),
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Course",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
)),
const SizedBox(
width: 40,
),
Container(
width: 400,
child: FutureBuilder(
future: ref
.child("/Exam/$_selectedexam/$_selectedsemester")
.get(),
builder:
(context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key,
child: Text(snap.key.toString())))
.toList(),
value: _selectedcourse,
onChanged: (String? newValue) {
setState(() {
_selectedcourse = newValue!;
print(_selectedcourse);
});
},
);
}),
),
],
),
],
),
),
);
below is my error message details. when I run it first time it works well but if I change value from fall to spring it gives me error
error display on output screen
error display on debug console

I got an error while displaying data in dependent drop down in real time database using flutter

import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
class homepg extends StatelessWidget {
const homepg({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("home"),
),
body: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var ref = FirebaseDatabase.instance.ref();
var _selectedexam;
var _selectedsemester;
var _selectedcourse;
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(15),
child: Center(
child: Column(
children: <Widget>[
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Exam ",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
),
const SizedBox(
width: 40,
),
Container(
width: 400,
child: FutureBuilder(
future: ref.child("/Exam").get(),
builder:
(context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key,
child: Text(snap.key.toString())))
.toList(),
value: _selectedexam,
onChanged: (String? newValue) {
setState(() {
_selectedexam = newValue!;
print(_selectedexam);
});
},
);
}),
),
],
),
const SizedBox(
height: 100,
),
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Semester",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
)),
const SizedBox(
width: 20,
),
Container(
width: 400,
child: FutureBuilder(
future: ref.child("/Exam/$_selectedexam").get(),
builder:
(context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key,
child: Text(snap.key.toString())))
.toList(),
value: _selectedsemester,
onChanged: (String? newValue) {
setState(() {
//_selectedexam = null;
_selectedsemester = newValue!;
print(_selectedsemester);
});
},
);
}),
),
],
),
const SizedBox(
height: 100,
),
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Course",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
)),
const SizedBox(
width: 40,
),
Container(
width: 400,
child: FutureBuilder(
future: ref
.child("/Exam/$_selectedexam/$_selectedsemester")
.get(),
builder:
(context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key,
child: Text(snap.key.toString())))
.toList(),
value: _selectedcourse,
onChanged: (String? newValue) {
setState(() {
_selectedcourse = newValue!;
print(_selectedcourse);
});
},
);
}),
),
],
),
],
),
),
);
}
}
Here is the complete code. I want to display data in a dependent dropdown but I got error kindly tell me how to solve it. In first drop down I display exam(spring and fall) if user clicks on spring it displays related semesters in another drop down. It works fine for first time but when I change drop down value error shown Thanks
drop downs structure
error image after changing first drop down value
From what i see is when you have selected the first dropdown and based on that you have selected the second dropdown. And now you change the first dropdown the second dropdown will give error. for that you have to do the following thing in the code.
FutureBuilder (
future: ref.child("/Exam/$_selectedexam").get(),
builder: (context, AsyncSnapshot<DataSnapshot> asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String> (
items: asyncSnapshot.data!.children
.map((snap) => DropdownMenuItem(
value: snap.key, child: Text(snap.key.toString())))
.toList(),
value: _selectedsemester,
onChanged: (String? newValue) {
setState(() {
_selectedexam = null;/*or you can check this as well _selectedexam = ''; */ // make it null for empty
_selectedsemester = newValue!;
print(_selectedsemester);
});
},
);
}),
you can check this and let me know if it works for you.
End Solution:
Main cause is when the item is changed the dependent dropdown should be bought to initial state.

Flutter how to user hint and value DropdownButton

While coding an app i realized, that if you use a hint: with the DropdownButton and a value you only see the value. After some research and trying to work my way around it i finally found a solution. Idk if this is helpful or not but i wanted to share this with you and maybe it does help you in your own project. But without further ado here is the "not functional code":
import 'package:flutter/material.dart';
void main() => runApp(const ButtonClass());
class ButtonClass extends StatefulWidget {
const ButtonClass({Key? key}) : super(key: key);
#override
State<ButtonClass> createState() => _ButtonClassState();
}
class _ButtonClassState extends State<ButtonClass> {
List<DropdownMenuItem<String>> get dropdownItems {
List<DropdownMenuItem<String>> menuItems = [
const DropdownMenuItem(child: Text("One"), value: "Option1"),
const DropdownMenuItem(child: Text("Two"), value: "Option2"),
const DropdownMenuItem(
child: Text("Three"),
value: "Option3",
),
const DropdownMenuItem(
child: Text("Four"),
value: "Option4",
),
const DropdownMenuItem(
child: Text("Five"),
value: "Option5",
),
];
return menuItems;
}
String selectedValue = "Option1";
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Container(
width: 200.0,
height: 200.0,
child: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
hint: const Center(
child: FittedBox(
fit: BoxFit.contain,
child: Text(
"Displayed Text",
style: TextStyle(
color: Colors.black,
fontSize: 30.0,
fontFamily: 'Arial',
),
),
),
),
items: dropdownItems,
value: selectedValue,
onChanged: (String? newValue) {
setState(() {
selectedValue = newValue!;
});
},
),
),
),
),
),
);
}
}
And here is the solution:
Change the
String selectedValue = "Option1";
to (example)
String? _selectedColor;
and also change
value: selectedValue,
onChanged: (String? newValue) {
setState(() {
selectedValue = newValue!;
});
},
to
value: _selectedColor,
onChanged: (String? newValue) {
setState(() {
_selectedColor= newValue!;
});
},
Here is the full main.dart file:
import 'package:flutter/material.dart';
void main() => runApp(const ButtonClass());
class ButtonClass extends StatefulWidget {
const ButtonClass({Key? key}) : super(key: key);
#override
State<ButtonClass> createState() => _ButtonClassState();
}
class _ButtonClassState extends State<ButtonClass> {
List<DropdownMenuItem<String>> get dropdownItems {
List<DropdownMenuItem<String>> menuItems = [
const DropdownMenuItem(child: Text("One"), value: "Option1"),
const DropdownMenuItem(child: Text("Two"), value: "Option2"),
const DropdownMenuItem(
child: Text("Three"),
value: "Option3",
),
const DropdownMenuItem(
child: Text("Four"),
value: "Option4",
),
const DropdownMenuItem(
child: Text("Five"),
value: "Option5",
),
];
return menuItems;
}
String? _selectedColor;
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Container(
width: 200.0,
height: 200.0,
child: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
hint: const Center(
child: FittedBox(
fit: BoxFit.contain,
child: Text(
"Displayed Text",
style: TextStyle(
color: Colors.black,
fontSize: 30.0,
fontFamily: 'Arial',
),
),
),
),
items: dropdownItems,
value: _selectedColor,
onChanged: (String? newValue) {
setState(() {
_selectedColor = newValue!;
});
},
),
),
),
),
),
);
}
}

Dropdown menu in flutter

I have tried to build a dropdown button and a menu with it, where the value will be selected from the dropdown menu. The code is as below:
String valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
DropdownButton(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 20.0,
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map((valueItem){
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
The error I'm facing is in the set state, where I've assigned newValue to the valueChoose.
A value of type 'Object?' can't be assigned to a variable of type 'String'.
Try changing the type of the variable, or casting the right-hand type to 'String'.
That is the error showing up for the newValue assinged in the set state. Please help regarding this, thanks in advance!
Below is the code, including the AlertDailog:
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String ? valueChoose;
List listItem = [
"A", "B", "C", "D", "E"
];
void assignPopup(BuildContext context) {
var alertDialog = AlertDialog(
content:
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children:[
Container(
child: Text(
'Action',
),
),
]
),
Row(
children:[
Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: TextField(
decoration: InputDecoration(
labelText: 'Please add any comments',
),
),
),
),
]
),
Row(
children:[
Container(
child: Text(
'Assign To',
),
),
]
),
Row(
children: [
Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40.0,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map<DropdownMenuItem<String>>((valueItem){
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
),
),
],
),
],
),
);
showDialog(
context: context,
builder: (BuildContext context) {
return alertDialog;
}
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
...
...
Container(
child: TextButton(
onPressed: (){
assignPopup(context);
},
child: Text(
'Assign',
),
),
),
);
}
}
From the data that you provided I have created a example where I have used the alertdialog and inside it there is a drop down
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
void assignPopup(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
content: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(children: [
Container(
child: Text(
'Action',
),
),
]),
Row(children: [
Expanded(
child: Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: TextField(
decoration: InputDecoration(
labelText: 'Please add any comments',
),
),
),
),
),
]),
Row(children: [
Container(
child: Text(
'Assign To',
),
),
]),
Row(
children: [
Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40.0,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem
.map<DropdownMenuItem<String>>((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
),
),
],
),
],
),
);
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: TextButton(
onPressed: () {
assignPopup(context);
},
child: Text(
'Assign',
),
),
),
),
);
}
}
So here in order to change dropdown value you have to use the StatefulBuilder which will change your dropdown value. you can check the above example and make changes as per your needs.
Please run the code to check the desired output.
Let me know if it works.
Specify the types of DropdownButton and that map
String? valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 20.0,
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map<DropdownMenuItem<String>>((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
)
Declare variables outside the build method.
String? valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
Inside the build method add the Dropdown widget, So the complete state class code would look like this,
class _DropdownViewState extends State<DropdownView> {
String? valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
#override
Widget build(BuildContext context) {
return DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 20.0,
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (String? newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map<DropdownMenuItem<String>>((valueItem){
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
);
}

Flutter DropdownButtonFormField selected background color

I have a DropdownButtonFormField that have few option to select. After select the option and open back the selection list, how to changes the background color of the previous selected option? To indicate that the previous selected option
String _selectedPicGroup;
static const _picGroup = [
'Group A',
'Group B',
'Group C',
'Group D',
];
SizedBox(
height: 78,
child: DropdownButtonFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
decoration: InputDecoration(
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[300],
),
hint: Text('Please select group'),
isExpanded: true,
isDense: true,
value: _selectedPicGroup,
items: _picGroup.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item),
);
}).toList(),
validator: (value) =>
value?.isEmpty ?? true ? 'Cannot Empty' : null,
onChanged: (selectedItem) => setState(
() {
_selectedPicGroup = selectedItem;
},
),
),
);
You can copy paste run full code below
You can use selectedItemBuilder and in items: check if (item == _selectedPicGroup) then return customize DropdownMenuItem
selectedItemBuilder: (BuildContext context) {
return _picGroup.map<Widget>((String item) {
print("$item");
return DropdownMenuItem(value: item, child: Text(item));
}).toList();
},
items: _picGroup.map((item) {
if (item == _selectedPicGroup) {
return DropdownMenuItem(
value: item,
child: Container(
height: 48.0,
width: double.infinity,
color: Colors.grey,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
item,
),
)),
);
} else {
return DropdownMenuItem(
value: item,
child: Text(item),
);
}
}).toList(),
working demo
full code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _selectedPicGroup;
static const _picGroup = [
'Group A',
'Group B',
'Group C',
'Group D',
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 78,
child: DropdownButtonFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
decoration: InputDecoration(
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[300],
),
hint: Text('Please select group'),
isExpanded: true,
isDense: true,
value: _selectedPicGroup,
selectedItemBuilder: (BuildContext context) {
return _picGroup.map<Widget>((String item) {
print("$item");
return DropdownMenuItem(value: item, child: Text(item));
}).toList();
},
items: _picGroup.map((item) {
if (item == _selectedPicGroup) {
return DropdownMenuItem(
value: item,
child: Container(
height: 48.0,
width: double.infinity,
color: Colors.grey,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
item,
),
)),
);
} else {
return DropdownMenuItem(
value: item,
child: Text(item),
);
}
}).toList(),
validator: (value) =>
value?.isEmpty ?? true ? 'Cannot Empty' : null,
onChanged: (selectedItem) => setState(
() {
_selectedPicGroup = selectedItem;
},
),
),
)
],
),
),
);
}
}