简体   繁体   English

如何根据第一个下拉列表中选择的选项创建一个下拉选项

[英]how make a DropDown option based on option selected in first dropdown

I have two dropdowns.我有两个下拉菜单。 the first permit to choose the country and the second the city that depends on the country.第一个许可证选择国家,第二个许可证取决于国家/地区。 Both are widgets build in different files but used in the another same file.两者都是在不同文件中构建但在另一个相同文件中使用的小部件。 my difficulties: How to retrieve the choice of countrie and how to transmit it when i am calling the city widget to use it to retrieve the cities of the chosen country because my variable _maselectionpays that contains the choice of the country is not declared in this widget我的困难:当我调用城市小部件以使用它来检索所选国家/地区的城市时,如何检索国家/地区的选择以及如何传输它,因为我的包含国家选择的变量 _maselectionpays 未在此小部件中声明

                          //debut
                 Listepayswidget(),
                 Listevillewidget(pays:_maselectionpays),

EDIT:编辑:

my code of widget country (pays)我的小部件国家代码(付费)

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';


class Listepayswidget extends StatefulWidget {
 Listepayswidget({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => new _ListepayswidgetState();

}
class _ListepayswidgetState extends State<Listepayswidget> {

   String _maselectionpays;
   List data=List();
  //List _allville= new List();
   Future<String> getpays() async {
  // paradisimo.000webhostapp.com
  final response = await http.post(ressource);

  var resbody= json.decode(response.body);
  setState((){
  data=resbody;
   });
  print (resbody);
  return "success";
 //datab=data.toString();
   }

    @override
   void initState(){
    super.initState();
    this.getpays();

}
  @override
   Widget build(BuildContext context) {
   return new Container(

              child: 

              new DropdownButton(


                items: data.map((item){
                  return new DropdownMenuItem(
                    child: new Text(item['libelle_pays']),
                    value: item['code_pays'].toString(),

                  );
                }).toList(),
                onChanged: (newVal){
                  setState(() {
                   _maselectionpays=newVal; 
                  });
                },
                value: _maselectionpays,
              ),
            );




  }
}

The code of widget city(ville) is similar than the code of widget country(pays).小部件城市(ville)的代码与小部件国家(支付)的代码相似。

This is my code of statefulwidget when I call widget pays这是我调用 widget pays 时的 statefulwidget 代码

import 'package:flutter/material.dart';
import 'package:paradisimmo/utilitaire/combo_offre.dart';
import 'package:paradisimmo/utilitaire/combo_pays.dart';




class EnregistreroffrePage extends StatefulWidget {
   EnregistreroffrePage({this.username});
final String username;
  @override
  State<StatefulWidget> createState() => new _EnregistreroffrePage();
}

class _EnregistreroffreData {
  String email = '';
  String password = '';

}

class _EnregistreroffrePage extends State<EnregistreroffrePage> {
  bool monVal = false;
  bool tuVal = false;
  bool wedVal = false;
  int _radioValue1 = -1;

 void _handleRadioValueChange1(int value) {
    setState(() {
      _radioValue1 = value;

      switch (_radioValue1) {
        case 0:

          //correctScore++;
          break;
        case 1:

          break;
        case 2:

          break;
      }
    });
  }

  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  _EnregistreroffreData _data = new  _EnregistreroffreData();

  void submit() {
    // First validate form.
    if (this._formKey.currentState.validate()) {
      _formKey.currentState.save(); // Save our form now.

    }
  }

  @override
  Widget build(BuildContext context) {
    //final Size screenSize = MediaQuery.of(context).size;

    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Enregistrer une offre'),
      ),
        body: Center(
            child: ListView(
              shrinkWrap: true,
              padding: EdgeInsets.all(15.0),
              children: <Widget>[
                Center(
                  child: Card(
                    elevation: 8.0,
                    child: Container(
                      padding: EdgeInsets.all(10.0),
                      child: Column(
                        children: <Widget>[
                           //debut
                  Listepayswidget(),
                  Listevillewidget(pays:_maselectionpays),

You need to make use of StatefulWidget for your input form.您需要将StatefulWidget用于您的输入表单。 When you select a country , then trigger setState() and that will rebuild the city drop-down widget.当您选择一个country ,然后触发setState() ,这将重建city下拉小部件。 You can find more examples on StatefulWidget here .您可以在此处找到有关StatefulWidget更多示例。 Here is sample code from google.这是来自谷歌的示例代码

Read your newly added code and here is what i think:阅读您新添加的代码,这是我的想法:

_maselectionpays .aka country need to be accessed from the statefullwidget city(Listevillewidget). _maselectionpays .aka 国家需要从 statefullwidget 城市(Listevillewidget)访问。 It is needed there so you can rebuild the city upon country selection .那里需要它,以便您可以根据国家/地区选择重建城市。 There is a couple of ways of doing this, but the easiest is to make maselectionpays a variable inside EnregistreroffrePage , then pass it to Listepayswidget(maselectionpays) and to Listevillewidget(maselectionpays) .有一对夫妇的这样做的方法,但最简单的就是让maselectionpays内部变量EnregistreroffrePage ,然后将它传递给Listepayswidget(maselectionpays)Listevillewidget(maselectionpays) When it is changed you call setState inside Listepayswidget and you shall use it inside Listevillewidget in the DropdownButton .当它改变时,你在Listepayswidget调用setState并且你应该在Listevillewidget中的DropdownButton使用它。 Would have used it as an key in a HashMap<key, value> data , the value would of course be a list of citys and the key is countries.会使用它作为一个keyHashMap<key, value> data ,数值当然会成为城市的名单,关键是国家。 So if the key changes, the country then the city-list changes.因此,如果密钥发生变化,则国家/地区然后城市列表也会发生变化。 Therefor Flutter would re-build the element.因此 Flutter 会重新构建元素。

For a better management of this could you should look into the BLoC-pattern , it's less code.为了更好地管理这一点,您应该查看BLoC-pattern ,它的代码更少。 Or look into RxDart , that uses observables and Streams for handling changes, also this uses less code.或者查看RxDart ,它使用observablesStreams来处理更改,这也使用更少的代码。

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

相关问题 如何更改下拉列表中选定选项的颜色? - How to change the color of selected option in the dropdown list? 在 Flutter 中,如何使用户选择的下拉菜单选项指向特定的路由? - In Flutter, how to make a user selected dropdown menu option leads to a specific route? 在多个下拉菜单中,如果已在 Flutter 中选择,如何禁用选项? - In multiple dropdown menu, how to disable an option if already selected in Flutter? 如何在下拉列表中获取所选选项的状态并在 Firebase 中显示 - How to get state of selected option in dropdown and display in Firebase 如何为我的下拉菜单添加自定义选项? - How to add custom option for my dropdown? 如何在 flutter 的下拉列表中添加删除选项 - how to add delete option in dropdown in flutter "Flutter:如何根据选择的第一个下拉按钮值设置第二个下拉按钮(多级相关按钮)的值?" - Flutter: How to set value of second dropdown button (multilevel dependent buttons) on the basis of first dropdown button value selected? 如何在 flutter 中制作这样的下拉菜单? - How to make this like of dropdown in flutter? 如何在颤振的下拉菜单中进行选择? - How to make selection in dropdown in flutter? 如何在 Flutter 中将下拉所选项目设置为 TextFormField - How to set dropdown selected item into TextFormField in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM