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(),);
  }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...