feat(ModalComponent): add static callbacks + pass down scrollDamping

This commit is contained in:
2025-06-04 00:31:02 +03:00
parent 046fc33d47
commit bce2356a8c
5 changed files with 32 additions and 4 deletions

View File

@@ -1,3 +1,10 @@
## 0.0.4
- `TextFieldComponent`: Support generic components for background/backgroundFocused
- `ModalComponent`: Support for `scrollDamping`
- `ModalComponent`: Add `onAfterLoadStatic` and `onBeforeUnloadStatic` as lifecycle callbacks across
all ModalComponent usages
## 0.0.3
- `ModalComponent`: All configurable properties are now mutable via getters and setters.

View File

@@ -31,7 +31,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.3"
version: "0.0.4"
flutter:
dependency: "direct main"
description: flutter

View File

@@ -32,7 +32,7 @@ class ScrollableAreaComponent extends PositionComponent
double _scrollVelocity = 0.0;
/// The amount of velocity reduction per second.
final double damping;
double damping;
/// A short buffer of recent drag deltas to calculate fling speed.
final List<_DragSample> _dragSamples = [];

View File

@@ -21,6 +21,7 @@ class ModalComponent extends PositionComponent with HasGameReference {
PositionComponent? _closeButton;
PositionComponent? _footer;
PositionComponent? _background;
double? _scrollDamping;
/// The callback to invoke after the modal has finished loading.
VoidCallback? onAfterLoad;
@@ -37,6 +38,12 @@ class ModalComponent extends PositionComponent with HasGameReference {
/// The text component for the title, if a title is provided.
late TextComponent? titleComponent;
/// Static callback for after load event, useful for initialization or registering the modal in a stack.
static void Function(ModalComponent modal)? onAfterLoadStatic;
/// Static callback for before unload event, useful for cleanup.
static void Function(ModalComponent modal)? onBeforeUnloadStatic;
/// Creates a [ModalComponent] with the given parameters.
///
/// [scrollContent] is required and specifies the content to display inside the modal.
@@ -54,6 +61,7 @@ class ModalComponent extends PositionComponent with HasGameReference {
/// [onAfterLoad] optionally specifies a callback after the modal has loaded.
/// [footer] optionally specifies a footer component to display at the bottom.
/// [defaultFooterHeight] specifies the default height of the footer (default is 32).
/// [scrollDamping] specifies the damping factor for scroll velocity (default is 500.0).
ModalComponent({
required PositionComponent scrollContent,
required Vector2 size,
@@ -70,6 +78,7 @@ class ModalComponent extends PositionComponent with HasGameReference {
PositionComponent? footer,
PositionComponent? background,
this.defaultFooterHeight = 32,
double scrollDamping = 500.0,
}) : _scrollContent = scrollContent,
_title = title,
_padding = padding,
@@ -80,6 +89,7 @@ class ModalComponent extends PositionComponent with HasGameReference {
_closeButton = closeButton,
_footer = footer,
_background = background,
_scrollDamping = scrollDamping,
super(size: size, position: position);
/// The content to be displayed inside the scrollable area of the modal.
@@ -155,6 +165,13 @@ class ModalComponent extends PositionComponent with HasGameReference {
rebuild();
}
/// The damping factor for scroll velocity, used in the scrollable area.
double get scrollDamping => _scrollDamping ?? 500.0;
set scrollDamping(double value) {
_scrollDamping = value;
scrollArea.damping = value;
}
@override
Future<void> onLoad() async {
await super.onLoad();
@@ -237,12 +254,16 @@ class ModalComponent extends PositionComponent with HasGameReference {
}
/// Displays the modal by adding it to the game's viewport.
/// Calls [onAfterLoadStatic] after loading.
Future<void> show(FlameGame game) async {
return game.camera.viewport.add(this);
await game.camera.viewport.add(this);
onAfterLoadStatic?.call(this);
}
/// Hides the modal by removing it from the game's viewport.
/// Calls [onBeforeUnloadStatic] before unloading.
void hide() {
onBeforeUnloadStatic?.call(this);
return game.camera.viewport.remove(this);
}
}

View File

@@ -1,6 +1,6 @@
name: flame_ui
description: A reusable UI component library for Flame games, including buttons, text fields, modals, lists, and layout helpers.
version: 0.0.3
version: 0.0.4
homepage: https://github.com/chenasraf/flame_ui
repository: https://github.com/chenasraf/flame_ui