Files
pipelines/app/lib/ui/input/arrow_key_pager.dart

41 lines
1.2 KiB
Dart

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
/// Left and right arrow keys step to the previous and next item (an
/// artifact, a changed file) wherever a keyboard is attached. A field that
/// takes the arrows itself (a text box) keeps them while it has focus.
class ArrowKeyPager extends StatelessWidget {
const ArrowKeyPager({
super.key,
required this.onPrevious,
required this.onNext,
this.onEscape,
required this.child,
});
/// Null at the first item.
final VoidCallback? onPrevious;
/// Null at the last item.
final VoidCallback? onNext;
/// Escape, when the surface can be dismissed (a viewer page). Absent means
/// Escape keeps whatever meaning the surrounding page gives it.
final VoidCallback? onEscape;
final Widget child;
@override
Widget build(BuildContext context) => CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.arrowLeft): () =>
onPrevious?.call(),
const SingleActivator(LogicalKeyboardKey.arrowRight): () =>
onNext?.call(),
if (onEscape != null)
const SingleActivator(LogicalKeyboardKey.escape): () =>
onEscape!.call(),
},
child: Focus(autofocus: true, child: child),
);
}