flutter - 如何显示依赖于颤动中另一个字符串的数据的数据?
问题描述
我有 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: () {},
),
),
)
],
)
]);
}
}
解决方案
您将使用 if 语句,如果条件为真,请执行以下操作:
if (userselection=='Havo'){
LEVELS.length(x) //x is the number of levels you want to show for example for 4 levels x=3
}
推荐阅读
- javascript - 基于键将一个数组嵌套到另一个数组中
- javascript - 如果使用 .js 将命令与用户名一起使用,则发送 dm
- excel - 根据时间线更改时值的总和是否不等于 0,为切片器创建过滤器
- python-3.x - 我想将一个列表拆分为同一个列表中的小列表
- php - 创建了一个向 sql 查询添加条件但由于“AND”语句而返回 0 个结果的搜索表单
- latex - 页面每个角落的图像
- c# - 如何比较单个数组的元素
- c# - 使用 ac#script 更改预制件的颜色时,它会显示在检查器中,但不会显示在游戏视图中
- java - 在每 N 个字符处为字符串添加分隔符 Android?
- java - 列出地图中的值