import 'dart:async'; import 'package:flutter/widgets.dart'; /// Keeps the current content during short switches without delaying completion. class ProjectSwitchRegion extends StatefulWidget { const ProjectSwitchRegion({ super.key, required this.pendingProjectId, required this.builder, }); final String? pendingProjectId; final Widget Function(BuildContext context, bool showDefaults) builder; @override State createState() => _ProjectSwitchRegionState(); } class _ProjectSwitchRegionState extends State { Timer? _timer; bool _showDefaults = false; @override void initState() { super.initState(); _reset(); } @override void didUpdateWidget(covariant ProjectSwitchRegion oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.pendingProjectId != widget.pendingProjectId) _reset(); } void _reset() { _timer?.cancel(); _showDefaults = false; if (widget.pendingProjectId != null) { _timer = Timer(const Duration(milliseconds: 150), () { if (mounted) setState(() => _showDefaults = true); }); } } @override Widget build(BuildContext context) => ExcludeFocus( excluding: widget.pendingProjectId != null, child: IgnorePointer( ignoring: widget.pendingProjectId != null, child: widget.builder(context, _showDefaults), ), ); @override void dispose() { _timer?.cancel(); super.dispose(); } }