Skip to content

Commit d4db4d3

Browse files
committed
修复离开组件时崩溃的BUG
1 parent cfbcf84 commit d4db4d3

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

lib/windows/settings/widgets/sub_panel_item.dart

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ class _RawGradientInputSubPanelItemState
450450
late Color _color2;
451451

452452
bool _showPicker = false;
453-
454453
OverlayEntry? _overlayEntry;
454+
bool _isDisposed = false;
455455

456456
@override
457457
void initState() {
@@ -496,9 +496,12 @@ class _RawGradientInputSubPanelItemState
496496
}
497497

498498
_showOverlay() {
499+
if (_isDisposed) return;
499500
final overlay = Overlay.of(context);
500501

501-
final renderBox = context.findRenderObject() as RenderBox;
502+
final renderBox = context.findRenderObject() as RenderBox?;
503+
if (renderBox == null || !renderBox.hasSize) return;
504+
502505
final offset = renderBox.localToGlobal(Offset.zero);
503506

504507
_showPicker = true;
@@ -511,8 +514,8 @@ class _RawGradientInputSubPanelItemState
511514
child: GradientPicker(
512515
show: _showPicker,
513516
title: widget.title,
514-
initialColor1: widget.initialColor1,
515-
initialColor2: widget.initialColor2,
517+
initialColor1: _color1,
518+
initialColor2: _color2,
516519
onClose: _removeOverlay,
517520
onColor1Changed: _onColor1Changed,
518521
onColor2Changed: _onColor2Changed,
@@ -525,30 +528,41 @@ class _RawGradientInputSubPanelItemState
525528
}
526529

527530
_onColor1Changed(Color color) {
528-
_color1 = color;
529-
widget.onColor1Changed(color);
531+
if (_isDisposed) return;
532+
setState(() {
533+
_color1 = color;
534+
});
535+
widget.onColor1Changed.call(color);
530536
}
531537

532538
_onColor2Changed(Color color) {
533-
_color2 = color;
534-
widget.onColor2Changed(color);
539+
if (_isDisposed) return;
540+
setState(() {
541+
_color2 = color;
542+
});
543+
widget.onColor2Changed.call(color);
535544
}
536545

537-
_removeOverlay() async {
538-
// reflect color changes
539-
setState(() {});
546+
Future<void> _removeOverlay() async {
547+
if (_isDisposed || _overlayEntry == null) return;
540548

541549
_showPicker = false;
542-
_overlayEntry?.markNeedsBuild();
543550

544-
await Future.delayed(transitionDuration);
551+
// Update state before removing overlay
552+
if (mounted) {
553+
setState(() {});
554+
}
545555

556+
// Remove overlay immediately without delay
546557
_overlayEntry?.remove();
547558
_overlayEntry = null;
548559
}
549560

550561
@override
551562
void dispose() {
563+
_isDisposed = true;
564+
_overlayEntry?.remove();
565+
_overlayEntry = null;
552566
if (_overlayEntry != null) _removeOverlay();
553567
super.dispose();
554568
}

0 commit comments

Comments
 (0)