Flutter:DropdownButton的弹出位置不正确

时间:2020-09-12 17:09:31

标签: 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) {},
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

尝试实现此代码,它一定可以工作的好运:)

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