Flutter DropdownButton - Add title for separating items
Solution 1
try this code in dartpad:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return HomeScreen();
}
}
class HomeScreen extends StatefulWidget {
@override
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
String dropdownValue;
List<Product> products = [
Product(name: 'sep1', type: 'sep'),
Product(name: 'milk', type: 'data'),
Product(name: 'oil', type: 'data'),
Product(name: 'sep2', type: 'sep'),
Product(name: 'suger', type: 'data'),
Product(name: 'salt', type: 'data'),
Product(name: 'sep3', type: 'sep'),
Product(name: 'potatoe', type: 'data'),
Product(name: 'tomatoe', type: 'data'),
Product(name: 'apple', type: 'data'),
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('text')),
body: Column(
children: [
Text('test'),
Expanded(
child: DropdownButton<String>(
value: dropdownValue,
items: products.map((value) {
return DropdownMenuItem(
value: value.name,
child: value.type == 'data'
? Text(value.name)
: Divider(
color: Colors.red,
thickness: 3,
),
);
}).toList(),
onChanged: (newValue) {
setState(() {
dropdownValue = newValue;
});
print('$newValue $dropdownValue');
},
),
),
],
),
);
}
}
class Product {
String name;
String type;
Product({this.name, this.type});
}
Solution 2
In addition to Ali Tenni's answer you can also subclass DropdownMenuItem
like this:
class DropdownMenuItemSeparator<T> extends DropdownMenuItem<T> {
DropdownMenuItemSeparator() : super(
enabled: false, // As of Flutter 2.5.
child: Container(), // Trick the assertion.
);
@override
Widget build(BuildContext context) {
return Divider(thickness: 3);
}
}
This will make the separator more narrow because otherwise the default build()
adds some min height.
UPD 2021-09-14 Below is no longer relevant as the feature is implemented in Flutter 2.5. The code above updated accordingly.
This solves one of the two problems. Unfortunately, the second and the larger problem is that the separator is still tappable, and the tap closes the dropdown. To address this, I submitted a feature request to Flutter: https://github.com/flutter/flutter/issues/75487
DoobieAsDave
I'm interested in digital / analog music productions as well as musical programming languages like Chuck and SuperCollider
Updated on December 16, 2022Comments
-
DoobieAsDave over 1 year
Im trying to implement a
DropdownButton
that can separate specific items from each other with a title (see desired output on image below, the titles are in green)Like in the example I would like to add these green titles to my existing
DropdownButton
, but did not find anything that showed me how to add something else than items to aDropdownButton
I also tried to switch from
DropdownButton
to aListView
withExpansionTile
but I would like to keep the behaviour of theDropdownButton
...Is it possible to add these titles to the
DropdownButton
items
? Or shourd I try to achive it by a other way? Im stuck at the moment and dont know how to proceed