如何解决Flutter:DropdownButton的弹出位置不正确
我的下拉按钮的弹出位置不正确。我不知道是什么引起了这个问题,弹出窗口在按钮的右侧移动,它与Row小部件和构建器有些相关。我在主分支上。在此处检查示例代码 DartPad Sample
import 'package:Flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',debugShowCheckedModeBanner: false,theme: ThemeData(
primarySwatch: Colors.blue,),builder: (context,child) => Scaffold(
body: Row(
children: [
Container(
width: 200,color: Colors.blue,child: Column(children: [
Text("Side Menu"),])
),Expanded(child: child),],initialRoute: "/",onGenerateRoute: (_) => MaterialPageRoute(
builder: (BuildContext context) => Center(
child: DropdownButton(
hint: Text("test"),value: 0,items: [
DropdownMenuItem(child: Text("test 1"),value: 0),DropdownMenuItem(child: Text("test 2"),value: 1),DropdownMenuItem(child: Text("test 3"),value: 2),onChanged: (value) {},);
}
}
解决方法
尝试实现此代码,它一定可以工作的好运:)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,home: Scaffold(
appBar: AppBar(title: const Text(_title)),body: Center(
child: MyStatefulWidget(),),);
}
}
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = 'One';
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue,icon: Icon(Icons.arrow_downward),iconSize: 24,elevation: 16,style: TextStyle(color: Colors.deepPurple),underline: Container(
height: 2,color: Colors.deepPurpleAccent,onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},items: <String>['One','Two','three','Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,child: Text(value),);
}).toList(),);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。