Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 如何更改下拉按钮';颤振中的s选择指标_Flutter_Selectedindex_Dropdownbutton - Fatal编程技术网

Flutter 如何更改下拉按钮';颤振中的s选择指标

Flutter 如何更改下拉按钮';颤振中的s选择指标,flutter,selectedindex,dropdownbutton,Flutter,Selectedindex,Dropdownbutton,我想在页面加载时更改我的下拉按钮所选索引,我该怎么做? 我的下拉按钮是PlateType对象的列表,我需要将所选索引更改为表示旧用户选择的某个索引。 以下是我的代码: DropdownButtonHideUnderline( child: ButtonTheme( alignedDropdo

我想在页面加载时更改我的
下拉按钮
所选索引,我该怎么做?
我的
下拉按钮
PlateType
对象的列表,我需要将所选索引更改为表示旧用户选择的某个索引。 以下是我的代码:

DropdownButtonHideUnderline(
                                                  child: ButtonTheme(
                                                      alignedDropdown: false,
                                                      child: new DropdownButton<PlateType>(
                                                        hint: new Text(
                                                          "حرف",
                                                          textAlign: TextAlign.center,
                                                          style: TextStyle(color: Colors.white),
                                                        ),
                                                        value: selectedPlate,
                                                        isExpanded: false,
                                                        iconSize: 30,
                                                        style: new TextStyle(
                                                            color: Colors.white,fontFamily: 'iransans',
                                                        ),
                                                        onChanged: (PlateType pt) {
                                                          setState(() {
                                                            selectedPlate = pt;
                                                          });
                                                        },
                                                        items: plates.map((PlateType p) {
                                                          return new DropdownMenuItem<PlateType>(
                                                            value: p,
                                                            child: new Text(
                                                              p.name,
                                                              textAlign: TextAlign.center,
                                                              style: new TextStyle(color: Colors.black),
                                                            ),
                                                          );
                                                        }).toList(),
                                                      )
                                                  )
                                              )
DropdownButtonHideUnderline(
孩子:ButtonTheme(
alignedDropdown:false,
孩子:新的下拉按钮(
提示:新文本(
"حرف",
textAlign:textAlign.center,
样式:TextStyle(颜色:Colors.white),
),
值:selectedPlate,
isExpanded:错,
iconSize:30,
样式:新文本样式(
颜色:颜色。白色,fontFamily:“伊朗人”,
),
变更后:(平板式pt){
设置状态(){
所选板=pt;
});
},
项目:图版(图版p){
返回新的DropdownMenuItem(
值:p,
儿童:新文本(
p、 名字,
textAlign:textAlign.center,
样式:新文本样式(颜色:Colors.black),
),
);
}).toList(),
)
)
)

若要在页面加载下拉列表中选择默认值,可以全局声明列表,然后设置默认值 在调用小部件之前,如以下所示:

import 'package:flutter/material.dart';
import 'package:flutter_app/models/User.dart';

List<User> userList = new List<User>();
User selectedUser;

class MyDropDownForm extends StatefulWidget {
  createState() {
    userList.add(new User(name: 'ABC', age: '25'));
    userList.add(new User(name: 'SDF', age: '24'));
    userList.add(new User(name: 'FGDG', age: '23'));
    userList.add(new User(name: 'PQR', age: '28'));
    userList.add(new User(name: 'XFGDF', age: '29'));
    userList.add(new User(name: 'WWW', age: '26'));
    userList.add(new User(name: 'HHH', age: '30'));
    userList.add(new User(name: 'RFD', age: '35'));
    selectedUser = userList[0];

    return StateKeeper();
  }
}

class StateKeeper extends State<MyDropDownForm> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {

    return Container(
        color: Colors.white,
        child: Center(
            child: Form(

              key: _formKey,
              child: Column(

                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[

                  DropdownButtonHideUnderline(
                      child: ButtonTheme(
                          alignedDropdown: false,
                          child: new DropdownButton<User>(
                            hint: new Text(
                              "حرف",
                              textAlign: TextAlign.center,
                              style: TextStyle(color: Colors.white),
                            ),
                            value: selectedUser,
                            isExpanded: false,
                            iconSize: 30,
                            style: new TextStyle(
                              color: Colors.white,fontFamily: 'iransans',
                            ),
                            onChanged: (User pt) {
                              setState(() {
                                selectedUser = pt;
                                print("Selected user " + selectedUser.name);
                              });
                            },
                            items: userList.map((User p) {
                              return new DropdownMenuItem<User>(
                                value: p,
                                child: new Text(
                                  p.name,
                                  textAlign: TextAlign.center,
                                  style: new TextStyle(color: Colors.black),
                                ),
                              );
                            }).toList(),
                          )
                      )
                  )


                ],

              ),

            ),
        ),

    );
  }

}
导入“包装:颤振/材料.省道”;
导入“包:flatter_app/models/User.dart”;
List userList=新列表();
用户选择用户;
类MyDropDownForm扩展StatefulWidget{
createState(){
添加(新用户(姓名:“ABC”,年龄:“25”);
添加(新用户(名称:'SDF',年龄:'24');
添加(新用户(姓名:'FGDG',年龄:'23');
添加(新用户(姓名:'PQR',年龄:'28');
添加(新用户(名称:'XFGDF',年龄:'29');
添加(新用户(姓名:'WWW',年龄:'26');
添加(新用户(姓名:'HHH',年龄:'30');
添加(新用户(名称:'RFD',年龄:'35');
selectedUser=userList[0];
返回StateKeeper();
}
}
类StateKeeper扩展状态{
final _formKey=GlobalKey();
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色,白色,
儿童:中心(
孩子:表格(
键:_formKey,
子:列(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
下拉按钮侧下划线(
孩子:ButtonTheme(
alignedDropdown:false,
孩子:新的下拉按钮(
提示:新文本(
"حرف",
textAlign:textAlign.center,
样式:TextStyle(颜色:Colors.white),
),
值:selectedUser,
isExpanded:错,
iconSize:30,
样式:新文本样式(
颜色:颜色。白色,fontFamily:“伊朗人”,
),
一旦更改:(用户pt){
设置状态(){
selectedUser=pt;
打印(“所选用户”+所选用户名称);
});
},
条目:userList.map((用户p){
返回新的DropdownMenuItem(
值:p,
儿童:新文本(
p、 名字,
textAlign:textAlign.center,
样式:新文本样式(颜色:Colors.black),
),
);
}).toList(),
)
)
)
],
),
),
),
);
}
}
用户模型类:

class User {
  String name;
  String age;

  User({
    this.name,
    this.age,
  });

  factory User.fromJson(Map<String, dynamic> json) => new User(
    name: json["name"],
    age: json["age"],
  );

  Map<String, dynamic> toJson() => {
    "name": name,
    "age": age,
  };
}
类用户{
字符串名;
弦年龄;
使用者({
这个名字,
这个年龄,
});
工厂用户.fromJson(映射json)=>新用户(
名称:json[“名称”],
年龄:json[“年龄”],
);
映射到JSON()=>{
“姓名”:姓名,
“年龄”:年龄,
};
}

关键注意事项是通过获取列表项来填充selectedUser对象