@@ -147,6 +147,46 @@ class _DropdownItemButton<T> extends StatefulWidget {
147147}
148148
149149class _DropdownItemButtonState <T > extends State <_DropdownItemButton <T >> {
150+ late CurvedAnimation _opacityAnimation;
151+
152+ @override
153+ void initState () {
154+ super .initState ();
155+ _setOpacityAnimation ();
156+ }
157+
158+ @override
159+ void didUpdateWidget (_DropdownItemButton <T > oldWidget) {
160+ super .didUpdateWidget (oldWidget);
161+ if (oldWidget.itemIndex != widget.itemIndex ||
162+ oldWidget.route.animation != widget.route.animation ||
163+ oldWidget.route.selectedIndex != widget.route.selectedIndex ||
164+ widget.route.items.length != oldWidget.route.items.length ||
165+ widget.route.dropdownStyle.openInterval.end !=
166+ oldWidget.route.dropdownStyle.openInterval.end) {
167+ _opacityAnimation.dispose ();
168+ _setOpacityAnimation ();
169+ }
170+ }
171+
172+ @override
173+ void dispose () {
174+ _opacityAnimation.dispose ();
175+ super .dispose ();
176+ }
177+
178+ void _setOpacityAnimation () {
179+ final double menuCurveEnd = widget.route.dropdownStyle.openInterval.end;
180+ final double unit = 0.5 / (widget.route.items.length + 1.5 );
181+ final double start =
182+ clampDouble (menuCurveEnd + (widget.itemIndex + 1 ) * unit, 0.0 , 1.0 );
183+ final double end = clampDouble (start + 1.5 * unit, 0.0 , 1.0 );
184+ _opacityAnimation = CurvedAnimation (
185+ parent: widget.route.animation! ,
186+ curve: Interval (start, end),
187+ );
188+ }
189+
150190 void _handleFocusChange (bool focused) {
151191 final bool inTraditionalMode =
152192 switch (FocusManager .instance.highlightMode) {
@@ -197,15 +237,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
197237
198238 @override
199239 Widget build (BuildContext context) {
200- final double menuCurveEnd = widget.route.dropdownStyle.openInterval.end;
201-
202240 final DropdownItem <T > dropdownItem = widget.route.items[widget.itemIndex];
203- final double unit = 0.5 / (widget.route.items.length + 1.5 );
204- final double start =
205- clampDouble (menuCurveEnd + (widget.itemIndex + 1 ) * unit, 0.0 , 1.0 );
206- final double end = clampDouble (start + 1.5 * unit, 0.0 , 1.0 );
207- final CurvedAnimation opacity = CurvedAnimation (
208- parent: widget.route.animation! , curve: Interval (start, end));
209241
210242 Widget child = Container (
211243 padding: (menuItemStyle.padding ?? _kMenuItemPadding)
@@ -230,7 +262,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
230262 : child,
231263 );
232264 }
233- child = FadeTransition (opacity: opacity , child: child);
265+ child = FadeTransition (opacity: _opacityAnimation , child: child);
234266 if (kIsWeb && dropdownItem.enabled) {
235267 child = Shortcuts (
236268 shortcuts: _webShortcuts,
0 commit comments