简体   繁体   English

单击 DropdownButton 时应用程序崩溃 (Flutter)

[英]App crashes when DropdownButton is clicked (Flutter)

So I created a DropdownButton in my app.所以我在我的应用程序中创建了一个 DropdownButton。 The thing is that whenever I click the dropdown, the app crashes.问题是,每当我单击下拉菜单时,应用程序就会崩溃。 I'm so confused because when I click other widgets like TextFormFields before clicking the DropdownButton it seems to work properly.我很困惑,因为当我在单击 DropdownButton 之前单击 TextFormFields 等其他小部件时,它似乎工作正常。

Error Message: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 581 pos 12: 'menuHeight == menuBottom - menuTop': is not true.错误消息: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 581 pos 12: 'menuHeight == menuBottom - menuTop': is not true.

Here's my DropdownButton:这是我的下拉按钮:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DropDownTry(),
    );
  }
}

class DropDownTry extends StatefulWidget {
  const DropDownTry({Key? key}) : super(key: key);

  @override
  _DropDownTryState createState() => _DropDownTryState();
}

class _DropDownTryState extends State<DropDownTry> {
  String dropdownValue = 'Male';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: DropdownButton<String>(
              value: dropdownValue,
              icon: const Icon(Icons.arrow_downward),
              iconSize: 24,
              elevation: 16,
              underline: SizedBox(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
              items: <String>['Male', 'Female']
                  .map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            )),
      ),
    );
  }
}

Try below code hope its help to you try to remove const keyword for SizedBox Widget尝试下面的代码希望它对您尝试删除SizedBox Widget 的const关键字有所帮助

Declare one String variable for default dropdown value为默认下拉值声明一个字符串变量

  String? dropdownValue;

Your Dropdown Lists你的下拉列表

List gender = [
    'Male',
    'Female',
    'Other',
  ];

Your Dropdown Widget你的下拉小部件

DropdownButtonHideUnderline(
              child: DropdownButton(
                hint: Text(
                  'Select Gender',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 15,
                  ),
                  textAlign: TextAlign.center,
                ),
                value: dropdownValue,
                onChanged: (String? genderNewValue) {
                  setState(
                    () {
                      dropdownValue = genderNewValue;
                    },
                  );
                },
                items: gender.map<DropdownMenuItem<String>>(
                  (value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(
                        value,
                        style: TextStyle(
                          fontSize: 15,
                        ),
                      ),
                    );
                  },
                ).toList(),
              ),
            ),

Your result screen:您的结果屏幕: 在此处输入图片说明 在此处输入图片说明

Wrap your dropdown code in SingleChildScrollView.将您的下拉代码封装在 SingleChildScrollView 中。

ex.前任。

return Scaffold(
  body: SingleChildScrollView(
    child:Center(
        child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: DropdownButton<String>(
              value: dropdownValue,
              icon: const Icon(Icons.arrow_downward),
              iconSize: 24,
              elevation: 16,
              underline: SizedBox(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
              items: <String>['Male', 'Female']
                  .map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            )),
      ),
  )
)

Mainly, don't make the DropDown very sticky to the top.主要是不要让 DropDown 对顶部很粘。 It likes some space above.它喜欢上面的一些空间。 Also, this happens due to the bad layout of the parent widgets.此外,这是由于父小部件的布局错误造成的。 Maybe u have made a column with a single child and this child is a stack and the crashed widget is inside the stack.也许您已经创建了一个包含单个子项的列,而这个子项是一个堆栈,而崩溃的小部件位于堆栈内。 Try to make a clearer layout of the parent widgets.尝试使父小部件的布局更清晰。 also, put the main parent of the screen in a Material Widget.此外,将屏幕的主要父级放在 Material Widget 中。 The problem is caused because the framework can't calculate the heights beyond the menu.问题是因为框架无法计算超出菜单的高度。

I got same error.我得到了同样的错误。 after struggling 2 days, I figured it out that the problem is about two factors.经过两天的挣扎,我发现问题出在两个因素上。 one is I used dropdown in showModalBottomSheet and second one is I didn't use appBar in scaffold where mydropdown located in. When i located my scaffold that contains my dropdown in, to another screen and add appBar.一个是我在 showModalBottomSheet 中使用了下拉菜单,第二个是我没有在 mydropdown 所在的脚手架中使用 appBar。当我找到包含我的下拉菜单的脚手架时,将其添加到另一个屏幕并添加 appBar。 it worked perfectly.它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM