I get errors when quickly navigating back and forth between pages using keyboard shortcuts
**Describe the bug**
I get errors when quickly navigating back and forth between pages using keyboard shortcuts (e.g. Ctrl+B then Ctrl+I).
**Reproduction code
```dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class AppShortcuts extends StatelessWidget {
final Widget child;
final VoidCallback? onBack;
final VoidCallback? onInsert;
const AppShortcuts({
super.key,
required this.child,
this.onBack,
this.onInsert,
});
@override
Widget build(BuildContext context) {
return CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.keyB, control: true): () =>
onBack != null ? onBack!() : Get.back(),
if (onInsert != null) ...{
const SingleActivator(LogicalKeyboardKey.keyI, control: true): () =>
onInsert!(),
}
},
child: Focus(
autofocus: true,
child: child,
),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialRoute: '/page1',
getPages: [
GetPage(
name: '/page1',
page: () => const Page1(),
),
GetPage(
name: '/page2',
page: () => const Page2(),
binding: BindingsBuilder(() {
Get.lazyPut<Page2Controller>(() => Page2Controller());
}),
),
],
);
}
}
class Page1 extends StatelessWidget {
const Page1({super.key});
@override
Widget build(BuildContext context) {
return AppShortcuts(
onInsert: () => Get.toNamed('/page2'),
child: Scaffold(
appBar: AppBar(title: const Text('Page 1')),
body: Text('data'),
),
);
}
}
class Page2Controller extends GetxController {
static Page2Controller get to => Get.find();
final formKey = GlobalKey<FormState>();
final textController = TextEditingController();
}
class Page2 extends GetView<Page2Controller> {
const Page2({super.key});
@override
Widget build(BuildContext context) {
return AppShortcuts(
child: Scaffold(
body: Form(
key: controller.formKey,
child: Center(
child: Column(
spacing: 10,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.all(10),
child: TextField(
controller: controller.textController,
),
),
Text('Page 2\nPress Ctrl+B / Ctrl+I quickly'),
],
),
),
),
),
);
}
}
```
**To Reproduce**
Steps to reproduce the behavior:
1. press ctrl + i (to go to page2)
2. press ctrl + b (to return to page1)
3. press ctrl + i quickly (to go to page2)
**Expected behavior**
When you do step 2 the controller should have been deleted and when you go to step 3 the controller should have been successfully initialized
**Screenshots**
https://github.com/user-attachments/assets/01c10e5a-69b7-4eba-8b73-8cfe24742296
**Flutter Version:**
3.41.6
**Getx Version:**
4.7.3
**Describe on which device you found the bug:**
Linux, Android Imin POS
**Minimal reproduce code**
I've written before
0 条评论