如何在Flutter中创建自定义下拉框

时间:2020-11-06 19:17:38

标签: flutter

如何在Flutter中创建以下布局? 。我尝试使用下拉小部件创建它。但是,我无法获得此布局。

是否可以给我一些有关相同布局的活跃程度的提示

谢谢

需要布局

enter image description here

点击

enter image description here

我的代码段

Widget _buildHeading() {
    return Padding(
      padding: const EdgeInsets.only(top: 22.5),
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height * 0.06,
        decoration: BoxDecoration(
          color: Palette.RANDSTAD_DARK_BLUE,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Padding(
              padding:
                  const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
              child: Text(
                "text bar",
                style: TextStyle(
                  color: Palette.WHITE,
                  height: 1.2,
                  fontSize: 20.0,
                ),
              ),
            ),

            //added
            Container(
                width: 130.0,
                child: DropdownButton<String>(
                  value: dropdownValue,
                  icon: Icon(Icons.arrow_downward),
                  iconSize: 24,
                  iconEnabledColor: Palette.WHITE,
                  dropdownColor: Palette.RANDSTAD_DARK_BLUE,
                  underline: SizedBox(),
                  onChanged: (String newValue) async {
                    setState(() {
                      dropdownValue = newValue;
                    });
                  },
                  items: <String>[
                    'English',
                    'Spanish',
                    'Dutch',
                    'Polish',
                    "French",
                    "German"
                  ].map<DropdownMenuItem<String>>((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child:
                          Text(value, style: TextStyle(color: Palette.WHITE)),
                    );
                  }).toList(),
                ))
            //end added
          ],
        ),
      ),
    );
  }

1 个答案:

答案 0 :(得分:1)

您可以在AppBar操作中使用PopUpMenuButton。该功能是在下面的示例代码中实现的。您可以将其粘贴到main.dart文件中并运行。

工作代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
    
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Hello World'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var language = 'english';

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text(widget.title), actions: <Widget>[
        PopupMenuButton(
          child: Row(
            children: [
              Text('$language | '),
              Icon(
                Icons.keyboard_arrow_down,
                color: Colors.white,
              )
            ],
          ),
          onSelected: (value) {
            setState(() {
              language = value;
            });

            print(value);
          },
          itemBuilder: (_) {
            return [
              PopupMenuItem(
                  value: '',
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            'change language',
                            style: TextStyle(
                              fontFamily: 'Roboto',
                              fontWeight: FontWeight.w500,
                              fontSize: 14,
                            ),
                          ),
                          Icon(
                            Icons.keyboard_arrow_up,
                            color: Colors.black,
                          )
                        ],
                      ),
                      Divider(
                        color: Colors.grey,
                        thickness: 2,
                      ),
                    ],
                  )),
              PopupMenuItem(
                value: 'english',
                child: Text(
                  'english',
                  style: TextStyle(
                      fontFamily: 'Roboto',
                      fontWeight: FontWeight.w500,
                      fontSize: 14,
                      color:
                          language == 'english' ? Colors.blue : Colors.black),
                ),
              ),
              PopupMenuItem(
                value: 'espanol',
                child: Text(
                  'espanol',
                  style: TextStyle(
                      fontFamily: 'Roboto',
                      fontWeight: FontWeight.w500,
                      fontSize: 14,
                      color:
                          language == 'espanol' ? Colors.blue : Colors.black),
                ),
              ),
              PopupMenuItem(
                value: 'nederlands',
                child: Text(
                  'nederlands',
                  style: TextStyle(
                      fontFamily: 'Roboto',
                      fontWeight: FontWeight.w500,
                      fontSize: 14,
                      color: language == 'nederlands'
                          ? Colors.blue
                          : Colors.black),
                ),
              ),
              PopupMenuItem(
                value: 'polski',
                child: Text(
                  'polski',
                  style: TextStyle(
                      fontFamily: 'Roboto',
                      fontWeight: FontWeight.w500,
                      fontSize: 14,
                      color: language == 'polski' ? Colors.blue : Colors.black),
                ),
              ),
            ];
          },
        ),
        const SizedBox(
          width: 12,
        )
      ]),
      body: new Center(
        child: Text('DEMO'),
      ),
    );
  }
}