【问题标题】:Flutter DropDownButton Popup 200px below ButtonFlutter DropDownButton Popup 200px below Button
【发布时间】:2020-04-30 04:57:54
【问题描述】:

我在 ListView 中使用带有自定义样式的 DropDownButton。我的问题是:PopupMenu 在 Button 下方大约 200-300px 打开,所以看起来下面的 Button 已经打开:

我用自定义样式包装了下拉菜单,但我已经尝试删除它,但没有任何效果。我也尝试使用普通的下拉按钮,但效果相同。 对应的构建:

    @override
Widget build(BuildContext context) {
    homeModel = Provider.of<HomeModel>(context);
    model = Provider.of<TransferModel>(context);
    navigator = Navigator.of(context);
    var items = model.items.entries.toList();
    return Container(
      color: Colors.white,
      child: ListView.builder(
        physics: BouncingScrollPhysics(),
        itemCount: model.items.entries.length,
        itemBuilder: (BuildContext context, int index) {
              return Padding(
                padding: const EdgeInsets.only(left: 30, right: 30, top: 10),
                child: CustomDropDown(
                  errorText: "",
                  hint: items[index].value["label"],
                  items: items[index]
                      .value["items"]
                      .asMap()
                      .map((int i, str) => MapEntry(
                          i,
                          DropdownMenuItem(
                            value: i,
                            child: Text(str is Map
                                ? str["displayName"].toString()
                                : str.toString()),
                          )))
                      .values
                      .toList()
                      .cast<DropdownMenuItem<int>>(),
                  value: items[index].value["selected"],
                  onChanged: (position) =>
                      model.selectItem(items[index].key, position),
                ),
              );
        },
      ),
    );

  }

自定义下拉菜单:

class CustomDropDown extends StatelessWidget {
  final int value;
  final String hint;
  final String errorText;
  final List<DropdownMenuItem> items;
  final Function onChanged;

  const CustomDropDown(
      {Key key,
      this.value,
      this.hint,
      this.items,
      this.onChanged,
      this.errorText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
              color: Colors.grey[100], borderRadius: BorderRadius.circular(30)),
          child: Padding(
            padding:
                const EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 5),
            child: DropdownButton<int>(
              value: value,
              hint: Text(
                hint,
                style: TextStyle(fontSize: 20),
                overflow: TextOverflow.ellipsis,
              ),
              style: Theme.of(context).textTheme.title,
              items: items,
              onChanged: (item) {
                onChanged(item);
              },
              isExpanded: true,
              underline: Container(),
              icon: Icon(Icons.keyboard_arrow_down),
            ),
          ),
        ),
        if (errorText != null) 
          Padding(
            padding: EdgeInsets.only(left: 30, top: 10),
            child: Text(errorText, style: TextStyle(fontSize: 12, color: Colors.red[800]),),
          )

      ],
    );
  }
}

编辑:我刚刚注意到,弹出窗口总是在屏幕中心打开。但我仍然不知道为什么会这样。

编辑 2:感谢@João Soares,我现在缩小了问题范围:我用 ListView 围绕 Widget,并使用 AnimatedContainer 来打开和关闭菜单。这个容器的填充似乎是罪魁祸首,但我不知道如何解决这个问题,因为我需要那个容器:(孩子是 ListView 小部件)

  class ContentSheet extends StatelessWidget {
  final Widget child;
  final bool isMenuVisible;

  const ContentSheet({Key key, this.child, this.isMenuVisible}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 50),
      child: AnimatedContainer(
        duration: Duration(milliseconds: 450),
        curve: Curves.elasticOut,
        padding: EdgeInsets.only(top: isMenuVisible ? 400 : 100),
        child: ClipRRect(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20), topRight: Radius.circular(20)),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(20), topRight: Radius.circular(20)),
              color: Colors.white,
            ),
            child: child
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 您找到解决问题的方法了吗?

标签: flutter dart dropdownbutton


【解决方案1】:

我已经使用下面的代码尝试了您的 CustomDropDown 小部件,它按预期工作,而下拉菜单在视图中显示较低。您代码中的其他内容可能会影响其位置。

class DropdownIssue extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _DropdownIssueState();
  }
}

class _DropdownIssueState extends State<DropdownIssue> {
  int currentValue = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.grey,
        child: Container(
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CustomDropDown(
                hint: 'hint',
                errorText: '',
                value: currentValue,
                items: [
                  DropdownMenuItem(
                    value: 0,
                    child: Text('test 0'),
                  ),
                  DropdownMenuItem(
                    value: 1,
                    child: Text('test 1'),
                  ),
                  DropdownMenuItem(
                    value: 2,
                    child: Text('test 2'),
                  ),
                ].cast<DropdownMenuItem<int>>(),
                onChanged: (value) {
                  setState(() {
                    currentValue = value;
                  });
                  print('changed to $value');
                }
              ),
            ],
          ),
        )
      ),
    );
  }
}

【讨论】:

  • hmm,也许它与它在 listView 中有关?
  • 不应该。 ListViews 是关于具有多个孩子的滚动行为。它不应该将您的内容推向特定的高度。由于无法查看更多涉及的代码,因此很难调试。
  • 看来你是对的,我已经在我周围的代码中尝试过你的代码,它也有同样的问题。我会再调查一些。谢谢你:)
  • 对不起,我无法为您提供解决方案。但是如果你更多的是你周围的代码,我可以尝试提供帮助。
  • 我在上面添加了更多代码并缩小了罪魁祸首
猜你喜欢
  • 2022-12-02
  • 2018-04-12
  • 1970-01-01
  • 2021-01-26
  • 2021-01-27
  • 2020-12-29
  • 2021-01-17
  • 2021-04-23
  • 1970-01-01
相关资源
最近更新 更多