I have DropdownButtonFormField when I click on it a DialogBox with a list of items should appear, so I can select one item and it should return the selected item in DropdownButtonFormField.I tried with DropdownMenuItem but I need with DialogBox so that it looks good. So basically I wanted to replace the DropdownMenuItem with DialogBox. and I don't know how to do that I'm new to flutter, please help me with this. below I will attach images that I want to get.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dialog Box and return value',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyHomePageState();
}
}
class MyHomePageState extends State<MyHomePage> {
String selectedLanguage =" ";
var javascript ="java";
var c = "C";
var cpp = "C++";
var python = "Python";
@override
Widget build(BuildContext context) {
return Scaffold (
appBar: AppBar(
title: Text("Flutter SimpleDialog Example")
),
body: Container(
color: Colors.grey,
child: Center (
child: Column (
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
color: Colors.white,
height: 50,
width: 150,
child: DropdownButtonFormField(
items: [],
hint: Text("Select"),
),
),
]
)
),
)
);
}
showMyAlertDialog(BuildContext context) {
// Create SimpleDialog
SimpleDialog dialog = SimpleDialog(
title: const Text('Select a Language:'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
// Close and return value
Navigator.pop(context, javascript);
},
child: Text(javascript)
),
SimpleDialogOption(
onPressed: () {
// Close and return value
Navigator.pop(context, c);
},
child: Text(c),
),
SimpleDialogOption(
onPressed: () {
// Close and return value
Navigator.pop(context, cpp);
},
child: Text(cpp),
),
SimpleDialogOption(
onPressed: () {
// Close and return value
Navigator.pop(context, python);
},
child: Text(python),
)
],
);
// Call showDialog function to show dialog.
Future futureValue = showDialog(
context: context,
builder: (BuildContext context) {
return dialog;
}
);
futureValue.then( (language) => {
this.setState(() {
this.selectedLanguage = language;
})
});
}
}