首页 > 解决方案 > 如何显示依赖于颤动中另一个字符串的数据的数据?

问题描述

我有 2 个选择框,显示一个 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: () {},
                  ),
                ),
              )
            ],
          )
        ]);
  }
}

标签: flutterdart

解决方案


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

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

推荐阅读