Actions
sample.packages.flutter.lib.src.widgets.actions.1035.
This example shows how keyboard interaction can be added to a custom control
that changes color when hovered and focused, and can toggle a light when
activated, either by touch or by hitting the X
key on the keyboard when
the "And Me" button has the keyboard focus (be sure to use TAB to move the
focus to the "And Me" button before trying it out).
This example defines its own key binding for the X
key, but in this case,
there is also a default key binding for [ActivateAction] in the default key
bindings created by [WidgetsApp] (the parent for [MaterialApp], and
[CupertinoApp]), so the ENTER
key will also activate the buttons.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class FadButton extends StatefulWidget {
const FadButton({
Key? key,
required this.onPressed,
required this.child,
}) : super(key: key);
final VoidCallback onPressed;
final Widget child;
@override
State createState() => _FadButtonState();
}
class _FadButtonState extends State {
bool _focused = false;
bool _hovering = false;
bool _on = false;
late final Map> _actionMap;
final Map _shortcutMap =
const {
SingleActivator(LogicalKeyboardKey.keyX): ActivateIntent(),
};
@override
void initState() {
super.initState();
_actionMap = >{
ActivateIntent: CallbackAction(
onInvoke: (Intent intent) => _toggleState(),
),
};
}
Color get color {
Color baseColor = Colors.lightBlue;
if (_focused) {
baseColor = Color.alphaBlend(Colors.black.withOpacity(0.25), baseColor);
}
if (_hovering) {
baseColor = Color.alphaBlend(Colors.black.withOpacity(0.1), baseColor);
}
return baseColor;
}
void _toggleState() {
setState(() {
_on = !_on;
});
}
void _handleFocusHighlight(bool value) {
setState(() {
_focused = value;
});
}
void _handleHoveHighlight(bool value) {
setState(() {
_hovering = value;
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _toggleState,
child: FocusableActionDetector(
actions: _actionMap,
shortcuts: _shortcutMap,
onShowFocusHighlight: _handleFocusHighlight,
onShowHoverHighlight: _handleHoveHighlight,
child: Row(
children: [
Container(
padding: const EdgeInsets.all(10.0),
color: color,
child: widget.child,
),
Container(
width: 30,
height: 30,
margin: const EdgeInsets.all(10.0),
color: _on ? Colors.red : Colors.transparent,
),
],
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('FocusableActionDetector Example'),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child:
TextButton(onPressed: () {}, child: const Text('Press Me')),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: FadButton(onPressed: () {}, child: const Text('And Me')),
),
],
),
),
);
}
}
SHARE: