I want to make an app with a gamepad, I succeeded in making a tap function but there is no tap effect like pressing a manual widget,
How do I make the widget button have an effect like a manual tap?
this is the code I made:
// ignore_for_file: unnecessary_brace_in_string_interps
import 'package:flutter/material.dart';
import 'package:gamepads/gamepads.dart';
class _MyHomePageState extends State<MyHomePage> {
int counter = 0;
GlobalKey floatingKey = GlobalKey();
@override
void initState() {
super.initState();
task();
}
Future<void> task() async {
Gamepads.events.listen(gamePadE);
}
void gamePadE(GamepadEvent gamepadEvent) {
try {
FloatingActionButton floatingActionButton = (floatingKey.currentWidget as FloatingActionButton);
floatingActionButton.onPressed!();
setState(() {
counter += 1;
});
} catch (e) {}
}
@override
Widget build(BuildContext context) {
return Scaffold(
// ---
// more code
// ---
floatingActionButton: FloatingActionButton(
key: floatingKey,
onPressed: () {
print("ON PRESSED");
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
How do I make the widget button have an effect like a manual tap?
You can move callback to function and call it:
Define it in FAB constructor:
Use it in gamepad events: