简体   繁体   English

创建自定义下拉菜单 flutter

[英]Creating a custom drop down menu flutter

I am trying to create a custom drop down menu for my app, I have a picture of what it should look like.我正在尝试为我的应用程序创建一个自定义下拉菜单,我有一张它应该是什么样子的图片。 I have been searching online to see if there are any similar ideas, although everything I'm finding does not support null safety and so I am running into problems.我一直在网上搜索是否有任何类似的想法,尽管我发现的所有内容都不支持 null 安全性,所以我遇到了问题。 If it comes down to it I will try and code my own drop down although if anyone could lead me in the right direction that would be great only because I'm new to flutter.如果归根结底,我将尝试编写自己的下拉列表,尽管如果有人能引导我朝着正确的方向前进,那将是非常棒的,因为我是 flutter 的新手。

Also I have tried using DropdownButton in flutter but it does not produce what I am looking for.我也尝试在 flutter 中使用 DropdownButton ,但它不会产生我想要的东西。

在此处输入图像描述

Use DropDown button class, here is a complete running code, you can run it in dartpad使用DropDown按钮class,这里有完整的运行代码,可以在dartpad中运行

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

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

static const String _title = 'Flutter Code Sample';

@override
Widget build(BuildContext context) {
return MaterialApp(
  title: _title,
  home: Scaffold(
    appBar: AppBar(title: const Text(_title)),
    body: const Center(
      child: MyStatefulWidget(),
     ),
   ),
 );
 }
}

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

@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = 'One';

@override
Widget build(BuildContext context) {
return DropdownButton<String>(
  value: dropdownValue,
  icon: const Icon(Icons.arrow_downward),
  elevation: 16,
  style: const TextStyle(color: Colors.deepPurple),
  underline: Container(
    height: 2,
    color: Colors.deepPurpleAccent,
  ),
  onChanged: (String? newValue) {
    setState(() {
      dropdownValue = newValue!;
    });
  },
  items: <String>['One', 'Two', 'Free', 'Four']
      .map<DropdownMenuItem<String>>((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
);
}
}

You can achieve that using DropdownButton2 .您可以使用DropdownButton2来实现。

Check out Example 3 of How to add different height items like dividers and check out Options to see all parameters you can use to customize DropdownButton2 to your design.查看How to add different height items like dividers )的示例 3,并查看Options以查看可用于自定义 DropdownButton2 到您的设计的所有参数。

Disclaimer: I am the author of the package mentioned above.免责声明:我是上述package的作者。

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

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