Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在颤振中自定义下拉列表?_Flutter_Dart - Fatal编程技术网

Flutter 如何在颤振中自定义下拉列表?

Flutter 如何在颤振中自定义下拉列表?,flutter,dart,Flutter,Dart,在颤振中,我试图修改下拉列表,如下所示 我已经尝试使用覆盖容器,但覆盖是采取从整个页面定位。如何从上一个小部件定位到覆盖 ```import 'package:flutter/material.dart'; List titles = ['a','b','c','d',]; class sample extends StatefulWidget { @override CountriesFieldState createState() => CountriesFieldState();

在颤振中,我试图修改下拉列表,如下所示

我已经尝试使用覆盖容器,但覆盖是采取从整个页面定位。如何从上一个小部件定位到覆盖

```import 'package:flutter/material.dart'; 
List titles = ['a','b','c','d',];

class sample extends StatefulWidget {
@override
CountriesFieldState createState() => CountriesFieldState();
}

class CountriesFieldState extends State<sample> {
final FocusNode _focusNode = FocusNode();
  OverlayEntry _overlayEntry;

final LayerLink _layerLink = LayerLink();

OverlayEntry _createOverlayEntry() {
RenderBox renderBox = context.findRenderObject();
var size = renderBox.size;
var offset = renderBox.localToGlobal(Offset.zero);
return OverlayEntry(
    builder: (context) => Positioned(
          child: Container(
            height: 240,
            width: 320,
            child: Scaffold(
              body: Container(
                margin: EdgeInsets.only(top: 8),
                //color: Colors.red,
                child: Column(
                  children: <Widget>[
                    new Expanded(
                        child: ListView.builder(
                      itemBuilder: (BuildContext context, int index) {
                        return new Container(
                          //width: (320/360)*screenWidth,

                          decoration: new BoxDecoration(
                              border: new Border.all(color: Colors.white),
                              color: Colors.white),
                          child: new ListTile(
                            //dense: true,
                            contentPadding: EdgeInsets.only(
                                bottom: 0, left: 15, top: 0),
                            onTap: () {
                              _overlayEntry.remove();
                            },
                            title: new Text(
                              titles[index],
                              textAlign: TextAlign.left,
                              style: new TextStyle(
                                  fontSize: 15,
                                  fontFamily: "IBM Plex Sans Medium",
                                  fontWeight: FontWeight.w500,
                                  color: const Color(0xFF999aab)),
                            ),
                          ),
                        );
                      },
                      itemCount: titles.length,
                    )),
                  ],
                ),
              ),
            ),
          ),
        ));
 }

 @override
 Widget build(BuildContext context) {
  return Scaffold(
  body: Column(
    children: [
      new InkWell(
          child: Align(
            alignment: Alignment.center,
            child: Container(
              margin: EdgeInsets.only(top: 100),
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                      width: 1.0, color: Colors.lightBlue.shade900),
                ),
                // color: Colors.red,
              ),
              height: 50.0,
              width: 300.0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[Text("abc")],
              ),
            ),
          ),
          onTap: () {
            this._overlayEntry = this._createOverlayEntry();
           Overlay.of(context).insert(this._overlayEntry);
          })
    ],
  ),
);
}
 }```
“`import'包:flatter/material.dart”;
列表标题=['a'、'b'、'c'、'd'、];
类示例扩展StatefulWidget{
@凌驾
CountriesFieldState createState()=>CountriesFieldState();
}
类CountriesFieldState扩展状态{
最终焦点节点_FocusNode=FocusNode();
过度进入(OverlayEntry);;
最终层链接_LayerLink=LayerLink();
OverlayEntry_createOverlayEntry(){
RenderBox RenderBox=context.findenderobject();
var size=renderBox.size;
var offset=renderBox.localToGlobal(offset.zero);
返回重叠入口(
生成器:(上下文)=>已定位(
子:容器(
身高:240,
宽度:320,
孩子:脚手架(
主体:容器(
页边距:仅限边集(顶部:8),
//颜色:颜色,红色,
子:列(
儿童:[
新扩展(
子项:ListView.builder(
itemBuilder:(构建上下文,int索引){
退回新货柜(
//宽度:(320/360)*屏幕宽度,
装饰:新盒子装饰(
边框:新边框。全部(颜色:颜色。白色),
颜色:颜色。白色),
孩子:新的ListTile(
//是的,
contentPadding:仅限EdgeInsets(
底部:0,左侧:15,顶部:0),
onTap:(){
_覆盖入口。移除();
},
标题:新文本(
标题[索引],
textAlign:textAlign.left,
样式:新文本样式(
尺寸:15,
fontFamily:“IBM Plex SAN介质”,
fontWeight:fontWeight.w500,
颜色:常量颜色(0xFF999aab)),
),
),
);
},
itemCount:titles.length,
)),
],
),
),
),
),
));
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
儿童:[
新墨水池(
子对象:对齐(
对齐:对齐.center,
子:容器(
页边距:仅限边缘集(顶部:100),
装饰:盒子装饰(
边界:边界(
底部:边界侧(
宽度:1.0,颜色:颜色。浅蓝色。阴影900),
),
//颜色:颜色,红色,
),
身高:50.0,
宽度:300.0,
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[文本(“abc”)],
),
),
),
onTap:(){
这个。_overlayEntry=这个。_createOverlayEntry();
覆盖.of(上下文).插入(本._覆盖入口);
})
],
),
);
}
}```
我已经使用覆盖的概念来实现这一点。
是否有任何其他解决方案可以实现自定义下拉菜单???

使用
DropdownButton
小部件,您可以将任何自定义小部件作为项目传递

Container(
                      width: 200,
                      child: DropdownButton(
                          isExpanded: true,
                          hint: Text('Select Working Time'),
                          value:  selectedVal,
                          items: List.generate(
                              titles.length, (i) {
                            return DropdownMenuItem(
                                value:titles[i],
                                child: Text(
                                  titles[i],
                                  style: Theme.of(context)
                                      .primaryTextTheme
                                      .caption,
                                ));
                          }),
                          onChanged: (c) {

                            selectedVal = c.toString().toLowerCase();
                            setState(() {});
                          }),
                    )

这将有助于实现你的目标

List titles = ['a','b','c','d',];

class Sample extends StatefulWidget {
@override
CountriesFieldState createState() => CountriesFieldState();
}

class CountriesFieldState extends State<Sample> {
final FocusNode _focusNode = FocusNode();
  OverlayEntry _overlayEntry;
 GlobalObjectKey  _globalObjectKey = GlobalObjectKey(ValueKey('a_key_that_different_from_any_other'));   // ADD THIS LINE
 
final LayerLink _layerLink = LayerLink();

OverlayEntry _createOverlayEntry() {
   RenderBox renderBox =_globalObjectKey.currentContext?.findRenderObject(); //EDIT THIS LINE
    var size = renderBox.size;
    var offset = renderBox.localToGlobal(Offset.zero);

return OverlayEntry(
    builder: (context) => Positioned(
          child: Container(
            height: 240,
            width: 320,
            child: Scaffold(
              body: Container(
                margin: EdgeInsets.only(top: 8),
                //color: Colors.red,
                child: Column(
                  children: <Widget>[
                    new Expanded(
                        child: ListView.builder(
                      itemBuilder: (BuildContext context, int index) {
                        return new Container(
                          //width: (320/360)*screenWidth,

                          decoration: new BoxDecoration(
                              border: new Border.all(color: Colors.white),
                              color: Colors.white),
                          child: new ListTile(
                            //dense: true,
                            contentPadding: EdgeInsets.only(
                                bottom: 0, left: 15, top: 0),
                            onTap: () {
                              _overlayEntry.remove();
                            },
                            title: new Text(
                              titles[index],
                              textAlign: TextAlign.left,
                              style: new TextStyle(
                                  fontSize: 15,
                                  fontFamily: "IBM Plex Sans Medium",
                                  fontWeight: FontWeight.w500,
                                  color: const Color(0xFF999aab)),
                            ),
                          ),
                        );
                      },
                      itemCount: titles.length,
                    )),
                  ],
                ),
              ),
            ),
          ),
        ));
 }


 @override
 Widget build(BuildContext context) {
  return Scaffold(
  body: Column(
    children: [
      new InkWell(
          child: Align(
            alignment: Alignment.center,
            child: Container(
              key: _globalObjectKey, // ADD THIS LINE
              margin: EdgeInsets.only(top: 100),
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                      width: 1.0, color: Colors.lightBlue.shade900),
                ),
                // color: Colors.red,
              ),
              height: 50.0,
              width: 300.0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[Text("abc")],
              ),
            ),
          ),
          onTap: () {
            this._overlayEntry = this._createOverlayEntry();
           Overlay.of(context).insert(this._overlayEntry);
          })
    ],
  ),
);
}
 }
List titles=['a','b','c','d',];
类示例扩展StatefulWidget{
@凌驾
CountriesFieldState createState()=>CountriesFieldState();
}
类CountriesFieldState扩展状态{
最终焦点节点_FocusNode=FocusNode();
过度进入(OverlayEntry);;
GlobalObjectKey _GlobalObjectKey=GlobalObjectKey(ValueKey(“与任何其他项不同的键”);//添加此行
最终层链接_LayerLink=LayerLink();
OverlayEntry_createOverlayEntry(){
RenderBox RenderBox=\u globalObjectKey.currentContext?.findenderObject();//编辑此行
var size=renderBox.size;
var offset=renderBox.localToGlobal(offset.zero);
返回重叠入口(
生成器:(上下文)=>已定位(
子:容器(
身高:240,
宽度:320,
孩子:脚手架(
主体:容器(
页边距:仅限边集(顶部:8),
//颜色:颜色,红色,
子:列(
儿童:[
新扩展(
子项:ListView.builder(
itemBuilder:(构建上下文,int索引){
退回新货柜(
//宽度:(320/360)*屏幕宽度,
装饰:新盒子装饰(
边框:新边框。全部(颜色:颜色。白色),
颜色:颜色。白色),
孩子:新的ListTile(
//是的,
contentPadding:仅限EdgeInsets(
底部:0,左侧:15,顶部:0),
onTap:(){
_覆盖入口。移除();
},
标题:新文本(
标题[索引],
textAlign:textAlign.left,
样式:新文本样式(
尺寸:15,
fontFamily:“IBM Plex SAN介质”,
fontWeight:fontWeight.w500,
颜色: