如何在flutter中显示依赖于另一个String的数据的数据?

时间:2020-07-23 09:08:01

标签: flutter dart

我有2个选择框,其中显示一个AlertDialog。第一个在alertdialog中显示一个listview.builder,带有“ Havo”或“ Vwo”选项。当用户选择Havo时,选项1-5必须显示在第二个警报对话框中。当用户选择Vwo时,必须在第二个警报对话框中显示选项1-6。

这是我保存数据的列表: (还保存其他数据,请忽略)

    const LEVELS = [
      Level(level: 'Vwo', subjects: [
        'Nederlandse taal en literatuur',
        'Engelse taal en literatuur',
        'Frans',
        'Duits',
        'Maatschappijleer',
        'Culturele en kunstzinnige vorming',
        'Lichamelijke opvoeding',
        'Grieks',
        'Latijn',
        'Wiskunde A',
        'Wiskunde C',
        'Wiskunde B',
        'Natuurkunde',
        'Scheikunde',
        'Biologie',
        'Economie',
        'Geschiedenis',
        'Informatica',
        'Bedrijfseconomie',
        'Spaans'
      ], years: [
        '1',
        '2',
        '3',
        '4',
        '5',
        '6'
      ]),
      Level(level: 'Havo', subjects: [
        'Nederlands',
        'Engels',
        'Mens en natuur',
        'Mens en maatschappij',
        'Kunst en cultuur',
        'Bewegen en sport',
        'Duits',
        'Frans',
      ], years: [
        '1',
        '2',
        '3',
        '4',
        '5'
      ])
    ];

这是我的代码的构建方式:

     import 'package:flutter/material.dart';
    
    import '../../models/studyOptionsModel.dart';
    
    class SetupScreenThreeForm extends StatefulWidget {
      @override
      _SetupScreenThreeFormState createState() => _SetupScreenThreeFormState();
    }
    
    class _SetupScreenThreeFormState extends State<SetupScreenThreeForm> {
      final _formKey = GlobalKey<FormState>();
      var _isLoginPage = false;
      var _schoolLevel = 'Niveau';
      var _classYear = 'Klas';
      var _bestSubjects = '';
    
      var selectionOptions = LEVELS.toList();
    
      //The alertdialog for setting the level
      createAlertDialog(BuildContext context, selectionType) {
        return showDialog(
            context: context,
            builder: (context) {
              return Dialog(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20)),
                  backgroundColor: Color(0xFF7E36EC),
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: selectionOptions.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: <Widget>[
                            InkWell(
                                onTap: () {
                                  setState(() {
                                    _schoolLevel = selectionOptions[index].level;
                                    print(_schoolLevel);
                                    Navigator.pop(context);
                                  });
                                },
                                child: ListTile(
                                    leading: Text(
                                  selectionOptions[index].level,
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontFamily: 'Poppins',
                                      fontWeight: FontWeight.w500,
                                      fontSize: 14),
                                ))),
                          ],
                        ),
                      );
                    },
                  ));
            });
      }

  @override
  Widget build(BuildContext context) {
    return Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              SizedBox(
                height: 20,
              ),
              Container(
                width: double.infinity,
                height: 50,
                child: InkWell(
                  child: RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25)),
                    color: Color(0xFF7E36EC),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        _schoolLevel,
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Poppins',
                            fontWeight: FontWeight.w500,
                            fontSize: 14),
                      ),
                    ),
                    onPressed: () {
                      createAlertDialog(context, 'level');
                    },
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Container(
                width: double.infinity,
                height: 50,
                child: InkWell(
                  child: RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25)),
                    color: Color(0xFF7E36EC),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        _classYear,
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Poppins',
                            fontWeight: FontWeight.w500,
                            fontSize: 14),
                      ),
                    ),
                    onPressed: () {
                      createAlertDialog(context, 'years');
                    },
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Container(
                width: double.infinity,
                height: 50,
                child: InkWell(
                  child: RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25)),
                    color: Color(0xFF212121),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        'Sterkste vakken',
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Poppins',
                            fontWeight: FontWeight.w500,
                            fontSize: 14),
                      ),
                    ),
                    onPressed: () {},
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Container(
                width: double.infinity,
                height: 50,
                child: InkWell(
                  child: RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25)),
                    color: Color(0xFF212121),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        'Minst sterkste vakken',
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Poppins',
                            fontWeight: FontWeight.w500,
                            fontSize: 14),
                      ),
                    ),
                    onPressed: () {},
                  ),
                ),
              )
            ],
          )
        ]);
  }
}

1 个答案:

答案 0 :(得分:1)

您将使用if语句,并且如果条件为true,请执行以下操作:

if (userselection=='Havo'){
LEVELS.length(x) //x is the number of levels you want to show for example for 4 levels x=3
}