flutter的下拉框DropdownButton的二级联动效果:

效果图2张:

     

  直接复制可运行。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AllStudent extends StatefulWidget {
  @override
  _AllStudentState createState() => _AllStudentState();
}

class _AllStudentState extends State<AllStudent> {

  var isChapterValue;

  String var_school_name ;
  String var_stu_name ;

  String select_school_name ;


  @override
  Widget build(BuildContext context) {

    List<School> schools = List();
    schools.add(School("yanta","dayanta"));
    schools.add(School("changan","changanqu"));
    schools.add(School("lianhu","lianhugongyuan"));
    
    List<Stu> getStu(String school_name){
      List<Stu> stuList = List();
      if("yanta"==school_name){
        stuList.add(Stu("yantayi","z"));
        stuList.add(Stu("yantaer","z"));
        stuList.add(Stu("yantasan","z"));
      }else if("lianhu"==school_name){

        stuList.add(Stu("lianhuyi","z"));
        stuList.add(Stu("lianhuer","l"));
        stuList.add(Stu("lianhusan","w"));
      }else if("changan"==school_name){
        stuList.add(Stu("changanyi","z"));
        stuList.add(Stu("changaner","l"));
        stuList.add(Stu("changansan","w"));
      }
      return stuList;
    }

    return Scaffold(
      appBar: AppBar(
        title: Text("3456789"),
      ),
      body: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          DropdownButton<String>(
            value: var_school_name,
            onChanged: (String school_name) {
              setState(() {
                var_school_name = school_name;
                select_school_name = school_name;
                var_stu_name =getStu(select_school_name)[0].name;

              });
            },
            hint: Text("请选择ss"),
            items: schools
                .map<DropdownMenuItem<String>>((School school) {
              return DropdownMenuItem<String>(
                value: school.school_name,
                child: Text(school.school_name),
              );
            })
                .toList(),
          ),
          DropdownButton<String>(
            hint: Text("请选择"),
            value: var_stu_name,
            onChanged: (String stu_name) {
              setState(() {
                var_stu_name = stu_name;
              });
            },
            items: getStu(select_school_name).map<DropdownMenuItem<String>>((Stu stu) {
              return DropdownMenuItem<String>(
                value: stu.name,
                child: Text(stu.name),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }
}

class School{
  String school_name;
  String school_address;
  School(this.school_name,this.school_address);
}
class Stu{
  String name;
  String indexLetter;
  Stu(this.name,this.indexLetter);
}

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐