how to display dropdown data based on dropdown number selection - flutter

I have a case where I have a dropdown where this dropdown value is created manually. Because the back end is made manually
Then what I want is, for example, when I select number 1 it displays data A, or I select number 2 it displays data B and so on.
What I want to ask is how do I get the data from the API based on the selected dropdown value.
This is the dropdown view and also the code
final List<String> list = <String>['1', '3', '5', '7'];
and when I select value from dropdown it displays data from API based
on dropdown selection as below
"data": {
"1": {
"id": "f732bbb0-a34a-474d-8829-23aa66470e22",
"id_dosen": "d6aedfb6-cf88-4e89-8365-0f206822a6c4",
"id_mk": "cb0bced5-a02d-4f46-bd88-6ed61daece10",
"nidn": null,
"dosen": "Yudhy",
"id_kelas_kuliah": "52deb32d-292f-44b9-af69-a90dfc5fbc81",
"kelas_kuliah": "Pendidikan agama islam III - Sistem Informasi - A",
"prodi": "Sistem Informasi",
"kelas": "KARYAWAN",
"semester": "5",
"kelompok_kelas": "A",
"kode": null,
"sks": 2,
"jumlah_kelas": 0,
"matakuliah": "Pendidikan agama islam III ( Islamic Religious Education III ) - A",
"smt": "2022-2023 GANJIL",
"bobot_sks": 2,
"rencana_pertemuan": 14,
"jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
"created_at": "2022-09-09 08:14:14",
"updated_at": "2022-09-09 08:14:14",
"created_by": "Fahmi Nugraha",
"updated_by": "Fahmi Nugraha"
} ...
What I want to ask is how do I make it.

The main concept is making dropdown button value nullable and set the sub-category to null when we will change the parent DropdownButton value.
DropdownButton(
value: mainValue,
items: list
.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(e),
),
)
.toList(),
onChanged: (value) {
subValue = null;
mainValue = value;
setState(() {});
},
),
DropdownButton(
value: subValue,
items: data[mainValue]
?.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(e),
),
)
.toList() ??
[],
onChanged: (value) {
subValue = value.toString();
setState(() {});
},
),
Play with this widget
class TGA extends StatefulWidget {
const TGA({super.key});
#override
State<TGA> createState() => _TGAState();
}
class _TGAState extends State<TGA> {
final List<String> list = <String>['1', '3'];
final data = {
'1': ["1A", "1B", "1C"],
'3': ["3A", "3B", "3C"],
};
String? mainValue;
String? subValue;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
DropdownButton(
value: mainValue,
items: list
.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(e),
),
)
.toList(),
onChanged: (value) {
subValue = null;
mainValue = value;
setState(() {});
},
),
DropdownButton(
value: subValue,
items: data[mainValue]
?.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(e),
),
)
.toList() ??
[],
onChanged: (value) {
subValue = value.toString();
setState(() {});
},
),
],
),
);
}
}

Related

How to assign initial value in DropdownButton in Flutter..?

suppose you a List of String List which will come from API.
now you want to show that list inside DropdownButton.
Let's say your list is like
homepage_categories": [
{
"id": 1,
"name": "Electronics",
"slug": "electronics",
"icon": "fas fa-anchor",
"image": "uploads/custom-images/electronics-2022-11-19-02-48-28-5548.png"
},
{
"id": 2,
"name": "Game",
"slug": "game",
"icon": "fas fa-gamepad",
"image": "uploads/custom-images/game-2022-11-19-02-48-48-6382.png"
},
{
"id": 3,
"name": "Mobile",
"slug": "mobile",
"icon": "fas fa-mobile-alt",
"image": "uploads/custom-images/mobile-2022-11-19-02-49-20-2538.png"
},
{
"id": 4,
"name": "Lifestyle",
"slug": "lifestyle",
"icon": "fas fa-home",
"image": "uploads/custom-images/lifestyle-2022-11-19-02-49-38-3139.png"
},
]
From this API you can easily fetch.
So the Question is How can i assign a default/initial value like Select Category or something else into that list
I have tried this...
late List<CategoryModel> category;
late String value;
#override
void initState() {
category = context.read<CategoryBrandCubit>().categoryBrandModel.category;
value = category.first.id.toString();
super.initState();
}
DropdownButton<String>(
hint: const Text('Select Status'),
isExpanded: true,
icon: const Icon(Icons.keyboard_arrow_down_rounded),
underline: const SizedBox(),
value: value,
onChanged: (String? val) {
value = val!;
bloc.add(StoreProductEventCategory(value));
print('catVal: $value');
},
items: category
.map(
(e) => DropdownMenuItem<String>(
value: e.id.toString(),
child: Text(e.name),
),
)
.toList(),
),
try this:
DropdownButton<String>(
hint: const Text('Select Status'),
isExpanded: true,
icon: const Icon(
Icons.keyboard_arrow_down_rounded),
underline: const SizedBox(),
value: "default",
onChanged: (String? val) {},
items: [
const DropdownMenuItem(
alignment: Alignment.center,
value: "default",
enabled: false,
child: Text("Select Your role"),
),
...category
.map(
(e) => DropdownMenuItem<String>(
value: e.id.toString(),
child: Text(e.name),
),
)
.toList()
],
),
The DropdownButton default value is corrosponding to the value field. If we take a look at your code we see that you assign value but it is empty:
late String value; // this is empty
DropdownButton<String>(
hint: const Text('Select Status'),
isExpanded: true,
icon: const Icon(Icons.keyboard_arrow_down_rounded),
underline: const SizedBox(),
value: value, // always the current value
onChanged: (String? val) {
value = val!;
bloc.add(StoreProductEventCategory(value));
print('catVal: $value');
},
What you have to do is asigning an initial value to value. You could do something like: value = homepage_categories[0].name
If you take a look at the official flutter documentation you can also see that the value field is described as the following:
value → T?: The value of the currently selected DropdownMenuItem.
You can read more about it here and see the full example.

Flutter Dependent Dropdown Error "Either zero or 2 or more [DropdownMenuItem]s were detected with the same value"

Good day.
I am encountering a problem wherein I get an error Either zero or 2 or more [DropdownMenuItem]s were detected with the same value when selecting back the default value of the parent dropdown.
For Example:
"Product" is the default value in dropdown and click both "Meal" and "Health" by swapping. I'll select now the "Services" and select both "Massage" and "Delivery" by swapping. Now, will go to "Product" then the error will be visible.
These are the variables that I used.
String? getStringType;
String? getStringCategory;
List<dynamic> dropDownItemType = [];
List<dynamic> categoryMasters = [];
List<dynamic> categories = [];
String? itemTypeId;
String? categoryId;
and here is my initState where the List Values are stored.
void initState() {
// TODO: implement initState
dropDownItemType.add({"id": 1, "name": "Product"});
dropDownItemType.add({"id": 2, "name": "Services"});
categoryMasters = [
{
"ID": 1,
"Name": "Meal",
"ParentId": 1,
},
{
"ID": 2,
"Name": "Health",
"ParentId": 1,
},
{
"ID": 3,
"Name": "Massage",
"ParentId": 2,
},
{
"ID": 4,
"Name": "Delivery",
"ParentId": 2,
},
];
super.initState();
}
This is my Parent Dropdown where the Child Dropdown depends on what value is going to be selected.
FormHelper.dropDownWidget(
context,
"Select Type",
this.itemTypeId,
this.dropDownItemType,
(onChangedVal) {
itemTypeId = onChangedVal;
getStringType = dropDownItemType[
int.parse(itemTypeId.toString()) - 1]
["name"];
print(
"ge: ${itemTypeId} and ${getStringType}");
this.categories = this
.categoryMasters
.where((categoryItem) =>
categoryItem["ParentId"].toString() ==
onChangedVal.toString())
.toList();
setState(() {
this.categoryId = null;
});
},
(onValidateVal) {
if (onValidateVal == null) {
return 'Select a Type';
}
return null;
},
borderFocusColor: const Color(0xFFCECECE),
prefixIconColor: const Color(0xFF808080),
borderColor: const Color(0xFFCECECE),
enabledBorderWidth: 1,
prefixIconPaddingLeft: 12,
borderRadius: 12,
paddingLeft: 0,
hintFontSize: 12,
paddingRight: 0,
contentPadding: 14,
showPrefixIcon: true,
borderWidth: 1,
prefixIcon: const Icon(Icons.type_specimen),
),
and this is the child dropdown wherein it depends on the parent dropdown's value.
FormHelper.dropDownWidget(
context,
"Select Category",
this.categoryId,
this.categories,
(onChangedVal) {
this.categoryId = onChangedVal;
setState(() {
this.categoryId = this
.categoryMasters[0]["ParentId"]
.toString();
});
getStringCategory = this.categoryMasters[
int.parse(categoryId.toString()) - 1]
["Name"];
print("Cat Name: ${getStringCategory}");
},
(onValidateVal) {
return null;
},
borderFocusColor: const Color(0xFFCECECE),
prefixIconColor: const Color(0xFF808080),
borderColor: const Color(0xFFCECECE),
enabledBorderWidth: 1,
prefixIconPaddingLeft: 12,
hintFontSize: 12,
borderRadius: 12,
paddingLeft: 0,
paddingRight: 0,
contentPadding: 14,
showPrefixIcon: true,
borderWidth: 1,
prefixIcon: const Icon(Icons.type_specimen),
),
Here, i have reference of dropdown list please refer it:
class _EmptySearchState extends State<EmptySearch> {
String? value1 = "";
List<String> nameList = [
"Name1",
"Name2",
"Name3",
"Name4",
"Name5",
"Name6",
"Name7",
"Name8"
];
#override
void initState() {
super.initState();
value1 = nameList[0];
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 2.0,
title: const Text('Hello'),
),
body: DropdownButton(
value: value1,
onChanged: (value) {
setState(() {
value1 = value.toString();
});
},
items: nameList.map(
(item) {
return DropdownMenuItem(
value: item,
child: Text(item),
);
},
).toList(),
),
backgroundColor: Colors.grey[200],
);
}
}

how to get API data inside map in flutter

how do I get the API data here based on the user's choice, for example if the user chooses number 3 then the data that is issued is the data that is at number 3 in the API
final List<String> list = <String>['Ganjil', 'Genap' ];
final data = {
'Ganjil': [
'1', //I want to call API data here
'3', //I want to call API data here
'5', //I want to call API data here
'7', //I want to call API data here
],
'Genap': [
'2', //I want to call API data here
'4', //I want to call API data here
'6', //I want to call API data here
'8',
],
};
i want to call data based on existing id in API. how do i do it. Thank
you so when the user selects number 1 it will display API data with ID number 1
Container(
padding: const EdgeInsets.only(left: 12, right: 8),
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: const Text(
'Pilih Semester',
),
isExpanded: true,
value: mainValue,
items: list
.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(
e,
style: regular5,
),
),
)
.toList(),
onChanged: (value) {
subValue = null;
mainValue = value;
setState(() {});
},
),
),
),
const SizedBox(
height: 16,
),
Text(
'Semester',
style: bold5,
),
const SizedBox(
height: 8,
),
Container(
padding: const EdgeInsets.only(left: 12, right: 8),
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text(
'Pilih Semester',
style: regular5,
),
isExpanded: true,
value: subValue,
items: data[mainValue]
?.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(
e,
style: regular5,
),
),
)
.toList() ??
[],
onChanged: (value) {
subValue = value.toString();
setState(() {});
},
),
),
),
and this respon json
{
"status": "success",
"code": "200",
"data": {
"1": {
"id": "f732bbb0-a34a-474d-8829-23aa66470e22",
"id_dosen": "d6aedfb6-cf88-4e89-8365-0f206822a6c4",
"id_mk": "cb0bced5-a02d-4f46-bd88-6ed61daece10",
"nidn": null,
"dosen": "Yudhy",
"id_kelas_kuliah": "52deb32d-292f-44b9-af69-a90dfc5fbc81",
"kelas_kuliah": "Pendidikan agama islam III - Sistem Informasi - A",
"prodi": "Sistem Informasi",
"kelas": "KARYAWAN",
"semester": "5",
"kelompok_kelas": "A",
"kode": null,
"sks": 2,
"jumlah_kelas": 0,
"matakuliah": "Pendidikan agama islam III ( Islamic Religious Education III ) - A",
"smt": "2022-2023 GANJIL",
"bobot_sks": 2,
"rencana_pertemuan": 14,
"jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
"created_at": "2022-09-09 08:14:14",
"updated_at": "2022-09-09 08:14:14",
"created_by": "Fahmi Nugraha",
"updated_by": "Fahmi Nugraha"
},
"2": {
"id": "3573bcf8-bf00-445b-91bb-8362e98f3e70",
"id_dosen": "d61b7164-cd6c-4bd9-8be8-d2a576790b9c",
"id_mk": "40f02349-887d-47c2-b190-9c5d62adf738",
"nidn": null,
"dosen": "Shadam Hussaeni",
"id_kelas_kuliah": "fb969bb3-e0d9-47ac-9ede-365c78e38994",
"kelas_kuliah": "Bahasa inggris III (Conversation) - Sistem Informasi - A",
"prodi": "Sistem Informasi",
"kelas": "KARYAWAN",
"semester": "5",
"kelompok_kelas": "A",
"kode": null,
"sks": 2,
"jumlah_kelas": 0,
"matakuliah": "Bahasa inggris III (Conversation) ( English III (Conversation) ) - A",
"smt": "2022-2023 GANJIL",
"bobot_sks": 2,
"rencana_pertemuan": 14,
"jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
"created_at": "2022-09-14 08:05:31",
"updated_at": "2022-09-14 08:05:31",
"created_by": "Risca Nurzantika",
"updated_by": "Risca Nurzantika"
}, ...
You can call the API in onChange with the new mainValue
onChanged: (value) {
subValue = value.toString();
callAPI(); // Call API here
setState(() {});
}

_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>

How do I go about fixing this issue cause I have tried all the solutions:
[ERROR:flutter/runtime/dart_vm_initializer.cc(39)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
late String selectedDestination = '';
late List destinationsList = [];
var isLoaded = false;
Future getAllDestinations() async {
String urlDestinations = "http://localhost/api/destinations/";
var response = await http.get(Uri.parse(urlDestinations));
if(response.statusCode == 200){
var jsonData = json.decode(response.body);
setState((){
destinationsList = jsonData;
});
}
print(destinationsList);
}
An the DropDown code is this
type here
DropdownButtonFormField(
decoration: InputDecoration(
prefixIcon: const Icon(
Icons.location_pin,
color: primary,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
label: const Text(
"Destination to *",
style: TextStyle(
fontSize: 16,
color: primary,
),
),
),
validator: (value) => value == null
? 'Field must not be empty'
: null,
isExpanded: true,
value: selectedDestination,
items: destinationsList.map((item) {
return DropdownMenuItem(
value: item['id'].toString(),
child:
Text(Text(item['name']).toString()),
);
}).toList(),
onChanged: (String? value) {
setState(() {
selectedDestination = value!;
});
},
)`
Sample API
{
"destinations": [
{
"id": 1,
"name": "Nairobi",
"city": "Nairobi",
"location": null,
"mobile": "0741761232",
"status": 1,
"created_at": "2022-10-24T11:57:51.000000Z",
"updated_at": "2022-10-24T11:57:51.000000Z"
},
{
"id": 2,
"name": "Voi",
"city": "Voi",
"location": null,
"mobile": "0741761235",
"status": 1,
"created_at": "2022-10-24T11:58:05.000000Z",
"updated_at": "2022-10-24T11:58:05.000000Z"
},
}
I just need to fetch the name. I am expecting to fetch a list of items in the dropdown.
The reason is you are directly assigning a map to a list when you are decoding the json. Idle way to do a json deserialization should be done using Model Classes. Example: https://docs.flutter.dev/cookbook/networking/fetch-data
To solve your problem you can do:
if(response.statusCode == 200){
var jsonData = json.decode(response.body);
setState((){
destinationsList = jsonData["destinations"];
});
You need to access the key which has list to assign it to the list of destinations. Like this jsonData["destinations"];

Convert data from an API to lists and use it in dependent Dropdownbuttons

I want to convert this API to multiable lists
it should act like the image below
https://i.stack.imgur.com/10Qld.png
Any help will be appreciated
I want to convert
1-categories.name toList
2-categories.children.name toList
3-categories.children.children_lv.name toList
4-categories.children.name.children_lv.children.name toList
and want to make every dropdown dependant to the previous one
Example:
the user should select from categories.name toList to be able to choose from 2-categories.children.name toList
API
"categories": [
{
"category_id": "1841",
"name": "Cars",
"children": [
{
"name": "Car1",
"category_id": "1845",
"children_lv": [
{
"name": "",
"category_id": "",
"children_lv": "",
"href": ""
}
],
"column": "1",
{
"name": "Car2",
"category_id": "1846",
"children_lv": [
{
"name": "Car2_1",
"category_id": "1847",
"children_lv": [
{
"name": "",
"category_id": "",
"children_lv": "",
"href": ""
}
],
},
{
"name": "Car2_2",
"category_id": "1848",
"children_lv": [
{
"name": "",
"category_id": "",
"children_lv": "",
"href": ""
}
],
}
],
}
],
},
{
"category_id": "1842",
"name": "Properties",
"children": [
{
"name": "",
"category_id": "",
"children_lv": "",
"href": ""
}
],
"column": "1",
},
{
"category_id": "1843",
"name": "Machines",
"children": [
{
"name": "",
"category_id": "",
"children_lv": "",
}
],
"column": "1",
},
{
"category_id": "1844",
"name": "Electronics",
"children": [
{
"name": "",
"category_id": "",
"children_lv": "",
"href": ""
}
],
"column": "1",
}
]
}```
the lists that should be converted are category, children and the other children_lv
**Model has been made with app.quicktype.io and it works **
List<T> list = (map['list'] as List).map<T>((e)=>T.fromMap(e));
Try with this, but replace T with Model.
Also it should have fromMap function, to parse Map<String,dynamic> to your model.
else use normal constructors
((e)=>T(a:e['a'], b:e['b']);
Here is an example:
class Model {
final String a;
final String b;
Model({this.a,this.b});
factory Model.fromMap(Map<String,dynamic> map) =>
Model(
a: map['a'],
b: map['b']
);
}
This is a model with fromMap function. You can get that function easy with plugins for AndroidStudio or VSCode.
The one I use for android studio is called DartDataClass.
Now when you have json with lists =>
{ list : [
{ "a":"we","b":"try"},
{ "a":"as","b":"dfg"},
]}
You can use code above to parse that JSON if you have create the model for it.
Map<String,dynamic> json = jsonDecode(jsonSource);
List<Model> list = (json['list'] as List).map<Model>((e)=>Model.fromMap(e)).toList();
I did an example, the same as your example, but changed some names to make it clear to you
First, you need to create the Classes that you need like this:
Example of only one class ***** the same thing applies to the other classes *****
myCategory.dart
import 'package:flutterapp/myCategory1.dart';
import 'package:json_annotation/json_annotation.dart';
part 'myCategory.g.dart';
#JsonSerializable()
class MyCategory {
String name;
List<MyCategory1> children1;
MyCategory({this.name,this.children1});
factory MyCategory.fromJson(Map<String, dynamic> json) =>
_$MyCategoryFromJson(json);
Map<String, dynamic> toJson() => _$MyCategoryToJson(this);
}
myCategory.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'myCategory.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
MyCategory _$MyCategoryFromJson(Map<String, dynamic> json) {
return MyCategory(
name: json['name'] as String,
children1: (json['children1'] as List)
?.map((e) => e == null
? null
: MyCategory1.fromJson(e as Map<String, dynamic>))
?.toList());
}
Map<String, dynamic> _$MyCategoryToJson(MyCategory instance) =>
<String, dynamic>{'name': instance.name, 'children1': instance.children1};
the myCategory.g.dart is generic file by json_serializable,
pubspec.yaml
dependencies:
flutter:
sdk: flutter
json_serializable: ^3.4.1
dev_dependencies:
build_runner: ^1.10.0
Where I generate it with the following command
flutter pub run build_runner build --delete-conflicting-outputs
Of course, you can use another method, but I prefer this method
Now let's take a look at the most important thing:
class CategoriesWidget extends StatefulWidget {
#override
_CategoriesWidgetState createState() => new _CategoriesWidgetState();
}
class _CategoriesWidgetState extends State<CategoriesWidget> {
List<MyCategory> myNestedList;
MyCategory _selectedMyCategory;
MyCategory1 _selectedMyCategory1;
MyCategory2 _selectedMyCategory2;
#override
void initState() {
super.initState();
myNestedList =
(map['categories'] as List).map((e) => MyCategory.fromJson(e)).toList();
}
Map<String, dynamic> map = {
'categories': [
{
"name": '1',
"children1": [
{"name": '1-1'},
{
"name": '1-2',
'children2': [
{'name': '1-2-1'},
{'name': '1-2-2'}
]
}
]
},
{
"name": '2',
"children1": [
{"name": '2-1'},
{
"name": '2-2',
'children2': [
{'name': '2-2-1'},
{'name': '2-2-2'}
]
}
]
}
]
};
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
title: Text("Dropdown Categories"),
centerTitle: true,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 45,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey, width: 0.5)),
padding: EdgeInsets.symmetric(horizontal: 2, vertical: 8),
margin: EdgeInsets.all(8),
child: DropdownButton(
isDense: true,
isExpanded: true,
hint: Text('My categories'),
underline: Container(),
items: myNestedList.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item.name),
);
}).toList(),
value: _selectedMyCategory,
onChanged: (value) {
setState(() {
_selectedMyCategory = value;
if (_selectedMyCategory1 != null) {
_selectedMyCategory1 = _selectedMyCategory.children1
.firstWhere(
(element) => element == _selectedMyCategory1,
orElse: () => null);
}
});
},
),
),
_selectedMyCategory?.children1 != null &&
_selectedMyCategory.children1.length > 0
? Container(
height: 45,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey, width: 0.5)),
padding: EdgeInsets.symmetric(horizontal: 2, vertical: 8),
margin: EdgeInsets.all(8),
child: DropdownButton(
isDense: true,
isExpanded: true,
hint: Text('My categories 1'),
underline: Container(),
value: _selectedMyCategory1,
items: _selectedMyCategory.children1.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item.name),
);
}).toList(),
onChanged: (value) {
setState(() {
_selectedMyCategory1 = value;
if (_selectedMyCategory2 != null) {
_selectedMyCategory2 =
_selectedMyCategory1.children2.firstWhere(
(element) =>
element == _selectedMyCategory2,
orElse: () => null);
}
});
},
),
)
: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(child: Text('My categories 1')),
),
_selectedMyCategory1?.children2 != null &&
_selectedMyCategory1.children2.length > 0
? Container(
height: 45,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey, width: 0.5)),
padding: EdgeInsets.symmetric(horizontal: 2, vertical: 8),
margin: EdgeInsets.all(8),
child: DropdownButton(
isDense: true,
isExpanded: true,
hint: Text('My categories 2'),
underline: Container(),
items: _selectedMyCategory1.children2.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item.name),
);
}).toList(),
value: _selectedMyCategory2,
onChanged: (value) {
setState(() {
_selectedMyCategory2 = value;
});
},
),
)
: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(child: Text('My categories 2')),
),
],
),
),
);
}
}
I tried it myself, and here is the result:
Good luck