-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew_Flutter_Dropdown.txt
More file actions
86 lines (80 loc) · 2.49 KB
/
New_Flutter_Dropdown.txt
File metadata and controls
86 lines (80 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? selectedParentValue;
String? selectedChildValue;
// Define the available parent and child dropdown values.
final List<String> parentValues = ['Electrical', 'Plumbing', 'Painting'];
final Map<String, List<String>> childValues = {
'Electrical': ['Wires', 'Swiches', 'Bulbs'],
'Plumbing': ['Tupes', 'Taps'],
'Painting': ['Paint', 'Brush', 'Paint Remover'],
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Parent Container Dropdown
DropdownButton<String>(
value: selectedParentValue,
hint: Text('Select The Category'),
onChanged: (newValue) {
setState(() {
selectedParentValue = newValue;
// When the parent value is changed, update the child value and reset the child dropdown
selectedChildValue = null;
});
},
items: parentValues.map((value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
SizedBox(height: 20),
// Child Container Dropdown
DropdownButton<String>(
value: selectedChildValue,
hint: Text('Sub Category'),
onChanged: (newValue) {
setState(() {
selectedChildValue = newValue;
});
},
items: selectedParentValue != null
? childValues[selectedParentValue]?.map((value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList()
: [], // Show empty list when no parent value is selected.
),
],
),
),
);
}
}