import 'package:flutter/material.dart'; class TusYayilmasiEngelleWidget extends StatefulWidget { final Widget child; final bool Function(KeyEvent event) shouldBlockKey; const TusYayilmasiEngelleWidget({ super.key, required this.child, required this.shouldBlockKey, }); @override _TusYayilmasiEngelleWidgetState createState() => _TusYayilmasiEngelleWidgetState(); } class _TusYayilmasiEngelleWidgetState extends State with WidgetsBindingObserver { late FocusNode _focusNode; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); _focusNode = FocusNode(canRequestFocus: true, skipTraversal: true,); _forceFocus(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); _focusNode.dispose(); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { Future.delayed(const Duration(milliseconds: 0), _forceFocus); } } void _forceFocus() { WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted || _focusNode.hasFocus) return; FocusScope.of(context).unfocus(); FocusScope.of(context).requestFocus(_focusNode); _focusNode.requestFocus(); }); } KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) { // int eventHash = event.hashCode; // Olayın benzersiz hash kodu // if (_lastEventHash == eventHash) { // return KeyEventResult.handled; // Aynı olay tekrar işlenmesin // } // _lastEventHash = eventHash; if (widget.shouldBlockKey(event)) { return KeyEventResult.handled; } return KeyEventResult.ignored; } @override Widget build(BuildContext context) { return Focus( focusNode: _focusNode, autofocus: true, canRequestFocus: true, skipTraversal: true, onKeyEvent: _handleKeyEvent, child: widget.child, ); } }