0

我试图这样做,但我得到一个错误。我想知道是否存在某种漏洞。另外,菜单有没有办法在显示值下方打开?

这是代码:

import 'package:flutter/material.dart';
import './grile_view.dart';

class Grile extends StatefulWidget {  

@override
  State<StatefulWidget> createState() {
    return GrileState();
  }
}

class GrileState extends State<Grile> {

  var _bGrile = ['bgrila 1', 'bgrila 2'];
  var _bio = 'bgrila 1';    

  @override
  Widget build(BuildContext context) {

    return Scaffold(

        body: Padding(

            padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0),
            child: ListView(
                children: <Widget>[
// DropdownButton
                  DropdownButton<String>(

                      items: _bGrile.map((String dropDownStringItem) {
                        return DropdownMenuItem<String>(
                            value: dropDownStringItem,
                            child: Text(dropDownStringItem)
                        );
                      }).toList(),

                      onChanged: (value){
                        Navigator.push(context, MaterialPageRoute(builder: (context) =>
                            GView()));
                      },
                      value: _bio
                  ),
// DropdownButton End
                ]
            )
        )
    );
  } // build
} // GrileState

我是编程新手,所以请原谅我在代码中写的任何愚蠢的东西。

4

1 回答 1

0

该类DropdownButton包含一个非常方便的hint属性。根据DropdownButton文档:

如果 [value] 为 null 且 [hint] 为非 null,则 [hint] 小部件显示为下拉按钮值的占位符。

这对你意味着什么?

您所要做的就是hint向您添加一个小部件DropdownButton,然后从您的动态值中删除分配。

DropdownButton下面是一个带有hint属性的 a 的简短示例:

class DropDownPage extends StatefulWidget {
  @override
  _DropDownPageState createState() => _DropDownPageState();
}

class _DropDownPageState extends State<DropDownPage> {
  int dropdownValue; // Notice how this isn't assigned a value.
  @override
  Widget build(BuildContext context) {
    return Center(
        child: DropdownButton(
      // Here's the hint property. I used a Text widget, 
      // but you can use any widget you like
      hint: Text("Pick a Widget"),
      value: dropdownValue,
      items: [
        DropdownMenuItem(child: Text("FlatButton"), value: 0),
        DropdownMenuItem(child: Text("Container"), value: 1),
        DropdownMenuItem(child: Text("Scaffold"), value: 2),
        DropdownMenuItem(child: Text("GestureDetector"), value: 3),
      ],
      onChanged: (value) {
        setState(() {
          dropdownValue = value;
        });
      },
    ));
  }
}

这是结果:

结果

因此,在您的代码中,这就是您必须在自己的代码中执行的操作。

编辑这个:var _bio = 'bgrila 1';

并将其更改为:var _bio;

然后将提示属性添加到您的DropdownButton

       DropdownButton<String>(
                hint: Text("Pick a *****"), // Replace with your hint widget
                items: _bGrile.map((String dropDownStringItem) {
                 return DropdownMenuItem<String>(
                     value: dropDownStringItem,
                   child: Text(dropDownStringItem)
                  );
                }).toList(),

                onChanged: (value){
                 Navigator.push(context, MaterialPageRoute(builder: (context) =>
                        GView()));
               },
               value: _bio
            ),

这样您的整个代码如下所示:

class Grile extends StatefulWidget {  

@override
  State<StatefulWidget> createState() {
    return GrileState();
  }
}

class GrileState extends State<Grile> {

  var _bGrile = ['bgrila 1', 'bgrila 2'];
  var _bio;    

  @override
  Widget build(BuildContext context) {

    return Scaffold(

        body: Padding(

            padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0),
            child: ListView(
                children: <Widget>[
// DropdownButton
                  DropdownButton<String>(
                    hint: Text("Pick a *******"),
                      items: _bGrile.map((String dropDownStringItem) {
                        return DropdownMenuItem<String>(
                            value: dropDownStringItem,
                            child: Text(dropDownStringItem)
                        );
                      }).toList(),

                      onChanged: (value){
                        Navigator.push(context, MaterialPageRoute(builder: (context) =>
                            GView()));
                      },
                      value: _bio
                  ),
// DropdownButton End
                ]
            )
        )
    );
  } // build
} // GrileState
于 2020-08-08T15:01:31.887 回答