如何在Flutter中的弹出菜单中实现下拉菜单

时间:2020-07-17 11:07:33

标签: flutter flutter-layout

我需要弹出菜单或任何其他方式来实现layout like this的抖动。我试图在弹出视图中实现它,但没有成功。请帮助我解决这个问题。

这是我尝试过的代码,使用其他任何方法实现也可以。这段代码给出了错误未找到重要的小部件,如果可以解决,这对我也有帮助。

void showFilter() {
    showGeneralDialog(
      barrierLabel: "Barrier",
      barrierDismissible: true,
      barrierColor: Colors.black.withOpacity(0.5),
      transitionDuration: Duration(milliseconds: 700),
      context: context,
      pageBuilder: (_, __, ___) {
        return Align(
          alignment: Alignment.center,
          child: Container(
            height: 300,
            child: SizedBox.expand(
                child:Column(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only( top: SizeConfig.blockSizeHorizontal*2, left: SizeConfig.blockSizeHorizontal*6),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Text("Monthly Summary", style: TextStyle(fontSize: SizeConfig.blockSizeHorizontal*6, color: Colors.black87,decoration: TextDecoration.none,),),
                        ],
                      )
                    ),
                    Container(
                      padding: EdgeInsets.only(top:SizeConfig.blockSizeHorizontal*2, left: SizeConfig.blockSizeHorizontal*3, right: SizeConfig.blockSizeHorizontal*3),
                      child: Divider(
                        color: Colors.black87,thickness: 2.0,
                      ),
                    ),
                        Container(
                            padding: EdgeInsets.only(left: SizeConfig.blockSizeHorizontal*6, top: SizeConfig.blockSizeHorizontal*4),
                            child: Row(
                              children: <Widget>[
                                Container(
                                  child: Row(
                                    children: <Widget>[
                                      Text("Year - ", style: TextStyle(fontSize: SizeConfig.blockSizeHorizontal*4.5, color: Colors.black87,decoration: TextDecoration.none,),)
                                    ],
                                  ),
                                ),
                                Container(
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                        child: DropdownButton(
                                          value: _dpdValue,
                                          underline: Container(),
                                          isExpanded: true,
                                          iconSize: 30.0,
                                          items: _dropdownValues.map((val) {
                                            return new DropdownMenuItem<String>(
                                              value: val,
                                              child: new Text(val),
                                            );
                                          }).toList(),
                                          onChanged: (val) {
                                            if (!mounted) return;
                                            setState(
                                                  () {
                                                _dpdValue = val;
                                              },
                                            );
                                          },
                                        ),
                                      )
                                    ],
                                  ),
                                )
                              ],
                            )
                        )
                      ],
                )
            ),
            margin: EdgeInsets.only(top: SizeConfig.blockSizeHorizontal*15, left: SizeConfig.blockSizeHorizontal*3, right: SizeConfig.blockSizeHorizontal*3),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(SizeConfig.blockSizeHorizontal*6),
            ),
          ),
        );
      },
      transitionBuilder: (_, anim, __, child) {
        return SlideTransition(
          position: Tween(begin: Offset(0, 1), end: Offset(0, 0)).animate(anim),
          child: child,
        );
      },
    );
  }

1 个答案:

答案 0 :(得分:1)

我为此使用了Alertdialog,希望它能为您解决问题提供见识,

 int year = 1990;
 List<String> months = ['January', 'February', 'March'];
  List<String> years = [];
  String option, month;

  @override
  void initState() {
    super.initState();
    for (int i = year; i < 2020; i++) {
      years.add(i.toString());
    }
  }

  Future<bool> errorDialog(context) async {
    Size size = MediaQuery.of(context).size;
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context, setState) {
            return AlertDialog(
              backgroundColor: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0))),
              content: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                     Padding(
                       padding: const EdgeInsets.all(8.0),
                       child: Text("Monthly Summary"),
                     ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text('Year-'),
                          Container(
                            width: size.width * 0.5,
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color:Colors.amber),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5))),
                            child: DropdownButtonFormField<String>(
                              decoration: InputDecoration(
                                  contentPadding:
                                      EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  filled: true,
                                  fillColor: Colors.white,
                                  hintText: 'Select Year',
                                  hintStyle:
                                      TextStyle(fontWeight: FontWeight.normal)),
                              value: option,
                              icon: Icon(Icons.arrow_drop_down),
                              iconSize: 24,
                              elevation: 16,
                              style: TextStyle(color: Colors.black),
                              onChanged: (String newValue) {
                                setState(() {
                                  option = newValue;
                                });
                              },
                              validator: (value) {
                                if (value == null) {
                                  return "Select Year";
                                }
                                return null;
                              },
                              items: years
                                  .map<DropdownMenuItem<String>>((String option) {
                                return DropdownMenuItem<String>(
                                  value: option,
                                  child: Text(option),
                                );
                              }).toList(),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text('Month-'),
                          Container(
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color:Colors.amber),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5))),
                            width: size.width * 0.5,
                            child: DropdownButtonFormField<String>(
                              decoration: InputDecoration(
                                  contentPadding:
                                      EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  filled: true,
                                  fillColor: Colors.white,
                                  hintText: 'Select Month',
                                  hintStyle:
                                      TextStyle(fontWeight: FontWeight.normal)),
                              value: month,
                              icon: Icon(Icons.arrow_drop_down),
                              iconSize: 24,
                              elevation: 16,
                              style: TextStyle(color: Colors.black),
                              onChanged: (String newValue) {
                                setState(() {
                                  month = newValue;
                                });
                              },
                              validator: (value) {
                                if (value == null) {
                                  return "Select Month";
                                }
                                return null;
                              },
                              items: months
                                  .map<DropdownMenuItem<String>>((String option) {
                                return DropdownMenuItem<String>(
                                  value: option,
                                  child: Text(option),
                                );
                              }).toList(),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: RaisedButton(
                              color: Colors.amber,
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(2.0))),
                              child: Text(
                                "CANCEL",
                                style: TextStyle(color: Colors.black),
                              ),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: RaisedButton(
                              color: Colors.amber,
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(2.0))),
                              child: Text(
                                "SEARCH",
                                style: TextStyle(color: Colors.black),
                              ),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          });
        });
  }