Files
flame/packages/flame/lib/src/components/input/button_component.dart
Lukas Klingsbo 5c47d7f6d7 chore: analyze issues introduced from new dart version (#1196)
* Added Component.childrenFactory

* fix some of the lint warnings

* more lint warnings

* remove changelog entry

* more analyzer warnings

* one more warning

* one more warning

* remove more unused imports

* fix more warnings

* another warning

* one more warning

* a lot more warnings

* some more warnings

* fix warnings in flame_svg

* fix warnings in flame_bloc

* Remove OrderedSet override feature

* Remove testRandom change

* Remove unnecessary type checks

* Re-remove deprecated argument in random_test

Co-authored-by: Pasha Stetsenko <stpasha@google.com>
2021-12-09 15:40:43 +01:00

84 lines
1.9 KiB
Dart

import 'package:meta/meta.dart';
import '../../../components.dart';
import '../../../input.dart';
/// The [ButtonComponent] bundles two [PositionComponent]s, one that shows while
/// the button is being pressed, and one that shows otherwise.
///
/// Note: You have to set the [button] in [onLoad] if you are not passing it in
/// through the constructor.
class ButtonComponent extends PositionComponent with Tappable {
late final PositionComponent? button;
late final PositionComponent? buttonDown;
/// Callback for what should happen when the button is pressed.
/// If you want to interact with [onTapUp] or [onTapCancel] it is recommended
/// to extend [ButtonComponent].
void Function()? onPressed;
ButtonComponent({
this.button,
this.buttonDown,
this.onPressed,
Vector2? position,
Vector2? size,
Vector2? scale,
double? angle,
Anchor? anchor,
int? priority,
}) : super(
position: position,
size: size ?? button?.size,
scale: scale,
angle: angle,
anchor: anchor,
priority: priority,
);
@override
@mustCallSuper
void onMount() {
assert(
button != null,
'The button has to either be passed in as an argument or set in onLoad',
);
final idleButton = button;
if (idleButton != null && !contains(idleButton)) {
add(idleButton);
}
}
@override
@mustCallSuper
bool onTapDown(TapDownInfo info) {
if (buttonDown != null) {
if (button != null) {
remove(button!);
}
add(buttonDown!);
}
onPressed?.call();
return false;
}
@override
@mustCallSuper
bool onTapUp(TapUpInfo info) {
onTapCancel();
return true;
}
@override
@mustCallSuper
bool onTapCancel() {
if (buttonDown != null) {
remove(buttonDown!);
if (button != null) {
add(button!);
}
}
return false;
}
}