我有DropdownButtonFormField,当我单击它时,应该会出现一个带有项目列表的 DialogBox,因此我可以选择一个项目,它应该在 DropdownButtonFormField 中返回所选项目。我尝试使用DropdownMenuItem,但我需要使用 DialogBox,以便它看起来不错。所以基本上我想用 DialogBox 替换 DropdownMenuItem。而且我不知道该怎么做我是新手,请帮助我。下面我将附上我想要的图像。
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;
})
});
}
}