gpt4 book ai didi

flutter - 根据另一个 DropdownButton [FLUTTER] 的值动态更改 DropdownButton 的值

转载 作者:行者123 更新时间:2023-12-02 01:52:34 29 4
gpt4 key购买 nike

希望你一切都好。我有一个小问题,我想根据另一个 DropdownButton 的值动态更改 DropdownButton 的值。

好吧,我会解释我的位置和问题所在:

首先,我有一个包含 3 个元素的 StudyCycle 列表;

final _studyCycles = const <StudyCycle>[
StudyCycle(cycle: Cycles.elementary, classrooms: <ClassroomsLevels>[
ClassroomsLevels.cp,
ClassroomsLevels.ce1,
ClassroomsLevels.ce2,
ClassroomsLevels.cm1,
ClassroomsLevels.cm2,
]),
StudyCycle(cycle: Cycles.primary, classrooms: <ClassroomsLevels>[
ClassroomsLevels.sixth,
ClassroomsLevels.fifth,
ClassroomsLevels.fourth,
ClassroomsLevels.third,
]),
StudyCycle(cycle: Cycles.secondary, classrooms: <ClassroomsLevels>[
ClassroomsLevels.second,
ClassroomsLevels.first,
ClassroomsLevels.terminal,
])
];

这个 StudyCycle 列表用于构建第一个 DropdownButton

 DropdownButton<StudyCycle>(
value: _selectedStudyCycle,
items: List<DropdownMenuItem<StudyCycle>>.generate(
_studyCycles.length,
(index) => DropdownMenuItem<StudyCycle>(
value: _studyCycles[index],
child: Text(_studyCycles[index].cycle.toString()),
),
),
hint: const Text('Your Study Cycle'),
onChanged: (StudyCycle? studyCycle) {
if (studyCycle != _selectedStudyCycle) {
if (_selectedStudyCycle != null) setState(() => _selectedStudyCycle = null);

if (studyCycle != null) setState(() => _selectedStudyCycle = studyCycle);
}
},
)

并且通过从 StudyCycles Dropdown 中选择一个 StudyCycle,它允许构建另一个包含 ClassroomsLevels 的 DropdownButton 的值 <当前在 StudyCycles 下拉列表 中选择的 strong>StudyCycle(到目前为止一切顺利)

    DropdownButton<ClassroomsLevels>(
hint: const Text('Select your classroom'),
value: _selectedClassroom,
items: _selectedStudyCycle == null
? []
: List<DropdownMenuItem<ClassroomsLevels>>.generate(
_selectedStudyCycle!.classrooms.length,
(index) => DropdownMenuItem<ClassroomsLevels>(
value: _selectedStudyCycle!.classrooms[index],
child: Text(_selectedStudyCycle!.classrooms[index].toString()),
),
),
onChanged: (ClassroomsLevels? classroom) => setState(() => _selectedClassroom = classroom),
)

The problem occurs when a ClassroomLevel is selected and I change StudyCycle, I get this error:

There should be exactly one item with [DropdownButton]'s value: ClassroomsLevels.fourth. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 915 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'

我尽力解决了这个问题,但我不能(而且我真的不明白为什么),所以我依赖你。你能帮我了解错误是什么以及如何解决吗?

完整代码

import 'package:flutter/material.dart';

class MyHomePage2 extends StatefulWidget {
const MyHomePage2({Key? key, required this.title}) : super(key: key);

final String title;

@override
State<MyHomePage2> createState() => _MyHomePage2State();
}

class _MyHomePage2State extends State<MyHomePage2> {
final _studyCycles = const <StudyCycle>[
StudyCycle(cycle: Cycles.elementary, classrooms: <ClassroomsLevels>[
ClassroomsLevels.cp,
ClassroomsLevels.ce1,
ClassroomsLevels.ce2,
ClassroomsLevels.cm1,
ClassroomsLevels.cm2,
]),
StudyCycle(cycle: Cycles.primary, classrooms: <ClassroomsLevels>[
ClassroomsLevels.sixth,
ClassroomsLevels.fifth,
ClassroomsLevels.fourth,
ClassroomsLevels.third,
]),
StudyCycle(cycle: Cycles.secondary, classrooms: <ClassroomsLevels>[
ClassroomsLevels.second,
ClassroomsLevels.first,
ClassroomsLevels.terminal,
])
];

StudyCycle? _selectedStudyCycle;

ClassroomsLevels? _selectedClassroom;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: SizedBox(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
DropdownButton<StudyCycle>(
value: _selectedStudyCycle,
items: List<DropdownMenuItem<StudyCycle>>.generate(
_studyCycles.length,
(index) => DropdownMenuItem<StudyCycle>(
value: _studyCycles[index],
child: Text(_studyCycles[index].cycle.toString()),
),
),
hint: const Text('Your Study Cycle'),
onChanged: (StudyCycle? studyCycle) {
if (studyCycle != _selectedStudyCycle) {
if (_selectedStudyCycle != null) setState(() => _selectedStudyCycle = null);

if (studyCycle != null) setState(() => _selectedStudyCycle = studyCycle);
}
},
),

//

DropdownButton<ClassroomsLevels>(
hint: const Text('Select your classroom'),
value: _selectedClassroom,
items: _selectedStudyCycle == null
? []
: List<DropdownMenuItem<ClassroomsLevels>>.generate(
_selectedStudyCycle!.classrooms.length,
(index) => DropdownMenuItem<ClassroomsLevels>(
value: _selectedStudyCycle!.classrooms[index],
child: Text(_selectedStudyCycle!.classrooms[index].toString()),
),
),
onChanged: (ClassroomsLevels? classroom) => setState(() => _selectedClassroom = classroom),
),
],
),
),
);
}
}

class StudyCycle {
final Cycles cycle;

final List<ClassroomsLevels> classrooms;

final String? description;

final int? iconCodePoint;

const StudyCycle({
required this.cycle,
required this.classrooms,
this.description,
this.iconCodePoint,
});
}

enum Cycles { elementary, primary, secondary, secondaryTechnique }

enum ClassroomsLevels {
cp,
ce1,
ce2,
cm1,
cm2,
sixth,
fifth,
fourth,
third,
second,
first,
terminal,
mechanical,
electronic,
electricity,
inEngineering,
secretariat,
accounting,
economy,
agr1,
agr2,
agr3,
}

谢谢!

最佳答案

错误的原因是,在您更改第二个下拉列表中的 _selectedClassroom 值后,新教室列表 _selectedStudyCycle!.classrooms 中不存在该值第一个下拉菜单。只需在 setState 方法中重置第二个下拉列表的值:

if (studyCycle != null) {
setState(() {
// Add this line
_selectedClassroom = null;
_selectedStudyCycle = studyCycle;
});
}

关于flutter - 根据另一个 DropdownButton [FLUTTER] 的值动态更改 DropdownButton 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70053961/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com