颤振中的下拉菜单
Drop down menu in flutter
在 Flutter.dev 网站上,您可以找到这种下拉体验。
如何设计这样的下拉菜单?
我试过关注但没有给出类似的体验。
PopupMenuButton(
child: Container(
margin: const EdgeInsets.all(10),
child: const Text(
'Main menu',
style: TextStyle(
fontSize: 16,
color: Colors.white,
decoration: TextDecoration.underline,
),
),
),
itemBuilder: (context) => [
PopupMenuItem(
child: const Text('Child one'),
value: 1,
onTap: () {},
),
PopupMenuItem(
child: const Text('Child two'),
value: 1,
onTap: () {},
),
],
)
以上代码给出了以下经验。
您可以使用此小部件 drop-down 按钮:https://pub.dev/packages/dropdown_button2
或者你可以参考这段代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter DropDownButton',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// Initial Selected Value
String dropdownvalue = 'Item 1';
// List of items in our dropdown menu
var items = [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
'Item 5',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Geeksforgeeks"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton(
// Initial Value
value: dropdownvalue,
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
),
],
),
),
);
}
}
在 Flutter.dev 网站上,您可以找到这种下拉体验。
如何设计这样的下拉菜单?
我试过关注但没有给出类似的体验。
PopupMenuButton(
child: Container(
margin: const EdgeInsets.all(10),
child: const Text(
'Main menu',
style: TextStyle(
fontSize: 16,
color: Colors.white,
decoration: TextDecoration.underline,
),
),
),
itemBuilder: (context) => [
PopupMenuItem(
child: const Text('Child one'),
value: 1,
onTap: () {},
),
PopupMenuItem(
child: const Text('Child two'),
value: 1,
onTap: () {},
),
],
)
以上代码给出了以下经验。
您可以使用此小部件 drop-down 按钮:https://pub.dev/packages/dropdown_button2
或者你可以参考这段代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter DropDownButton',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// Initial Selected Value
String dropdownvalue = 'Item 1';
// List of items in our dropdown menu
var items = [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
'Item 5',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Geeksforgeeks"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton(
// Initial Value
value: dropdownvalue,
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
),
],
),
),
);
}
}