Skip to content

Commit

Permalink
add close button functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
singlapriyanka315 committed Aug 14, 2024
1 parent 3e40821 commit 27647db
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 69 deletions.
10 changes: 3 additions & 7 deletions example/lib/widgets/components/widgets/modal_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,9 @@ GSModal(
size: selectedSizeOption,
content: GSModalContent(
header: GSModalHeader(
closeButton: GSModalCloseButton(
icon: const GSIcon(icon: Icons.close),
onPressed: () {
setState(() {
isOpen = false;
});
},
closeButton: const GSModalCloseButton(
icon: GSIcon(icon: Icons.close),

),
child: GSText(
text: "Invite your team",
Expand Down
23 changes: 8 additions & 15 deletions example/lib/widgets/storybook/widgets/modal_story.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ final class ModalStory extends StoryWidget {
size: GSModalSizes.values[context.knobs
.options(label: 'Size', initial: 0, options: sizeOptions)],
content: GSModalContent(
header: GSModalHeader(
closeButton: GSModalCloseButton(
icon: const GSIcon(icon: Icons.close),
onPressed: () {},
header: GSModalHeader(
closeButton: const GSModalCloseButton(
icon: GSIcon(icon: Icons.close),
),
style: GSStyle(
textStyle: TextStyle(
color: GSTheme.of(context).background900,
fontWeight: FontWeight.bold
)
),
textStyle: TextStyle(
color: GSTheme.of(context).background900,
fontWeight: FontWeight.bold)),
child: const GSText(
text: "Invite your team",
),
Expand All @@ -44,18 +41,14 @@ final class ModalStory extends StoryWidget {
child: const GSText(
text: "Cancel",
),
onPressed: () {
// context.pop();
}),
onPressed: () {}),
GSButton(
action: GSButtonActions.primary,
variant: GSButtonVariants.solid,
child: const GSText(
text: "Explore",
),
onPressed: () {
// context.pop();
}),
onPressed: () {}),
],
),
),
Expand Down
72 changes: 38 additions & 34 deletions lib/src/widgets/gs_modal/gs_modal.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:gluestack_ui/gluestack_ui.dart';
import 'package:gluestack_ui/src/style/style_resolver.dart';
import 'package:gluestack_ui/src/widgets/gs_modal/gs_modal_backdrop_style.dart';
import 'package:gluestack_ui/src/widgets/gs_modal/gs_modal_provider.dart';
import 'package:gluestack_ui/src/widgets/gs_modal/gs_modal_style.dart';
import 'package:gluestack_ui/src/widgets/gs_style_builder/gs_style_builder.dart';

Expand Down Expand Up @@ -89,43 +90,46 @@ class _GSModalState extends State<GSModal> {
inlineStyle: widget.style,
);

return GSAncestor(
decedentStyles: styler.descendantStyles,
child: Stack(
children: [
MouseRegion(
cursor: SystemMouseCursors.basic,
child: GestureDetector(
onTap: () {
if (widget.barrierDismissible == true) {
_removeModal();
}
},
child: Container(
color: widget.showBackdrop
? backdropStyler.bg
?.getColor(context)
.withOpacity(0.5) ??
const Color.fromRGBO(0, 0, 0, 0.5)
: const Color.fromRGBO(0, 0, 0, 0),
return GSModalProvider(
removeModal: _removeModal,
child: GSAncestor(
decedentStyles: styler.descendantStyles,
child: Stack(
children: [
MouseRegion(
cursor: SystemMouseCursors.basic,
child: GestureDetector(
onTap: () {
if (widget.barrierDismissible == true) {
_removeModal();
}
},
child: Container(
color: widget.showBackdrop
? backdropStyler.bg
?.getColor(context)
.withOpacity(0.5) ??
const Color.fromRGBO(0, 0, 0, 0.5)
: const Color.fromRGBO(0, 0, 0, 0),
),
),
),
),
GestureDetector(
onTap: () {
// Do nothing to prevent the modal from closing when tapped
},
child: Align(
alignment: styler.alignment ?? Alignment.center,
child: SizedBox(
width: (styler.modal?.maxWidth ??
1 * (styler.modal?.width ?? 1)),
height: styler.modal?.height,
child: widget.content,
GestureDetector(
onTap: () {
// Do nothing to prevent the modal from closing when tapped
},
child: Align(
alignment: styler.alignment ?? Alignment.center,
child: SizedBox(
width: (styler.modal?.maxWidth ??
1 * (styler.modal?.width ?? 1)),
height: styler.modal?.height,
child: widget.content,
),
),
),
),
],
],
),
),
);
},
Expand All @@ -135,7 +139,7 @@ class _GSModalState extends State<GSModal> {
);
}

void _removeModal() {
_removeModal() {
// Guard clause to prevent double execution
if (!_isOpen || _overlayEntry == null) {
return; // Exit if modal is already closed or there's no overlay to remove
Expand Down
11 changes: 5 additions & 6 deletions lib/src/widgets/gs_modal/gs_modal_close_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/gestures.dart';
import 'package:gluestack_ui/gluestack_ui.dart';
import 'package:gluestack_ui/src/style/style_resolver.dart';
import 'package:gluestack_ui/src/widgets/gs_modal/gs_modal_close_button_style.dart';
import 'package:gluestack_ui/src/widgets/gs_modal/gs_modal_provider.dart';

/// predefined sizes for [GSModalCloseButton], providing a consistent set of size options for icon buttons.
enum GSModalCloseButtonSizes {
Expand All @@ -18,9 +19,6 @@ class GSModalCloseButton extends StatelessWidget {
/// The icon to display within the button.
final GSIcon icon;

/// The callback that is called when the button is tapped.
final VoidCallback onPressed;

/// The callback that is called when the button is long-pressed.
final VoidCallback? onLongPress;

Expand All @@ -44,7 +42,6 @@ class GSModalCloseButton extends StatelessWidget {
const GSModalCloseButton({
super.key,
required this.icon,
required this.onPressed,
this.onLongPress,
this.onDoubleTap,
this.style,
Expand All @@ -71,12 +68,14 @@ class GSModalCloseButton extends StatelessWidget {
inlineStyle: style,
);

// widget.
final removeModal = GSModalProvider.of(context)?.removeModal;

return GSButton(
variant: variant ?? GSButtonVariants.link,
action: action ?? GSButtonActions.primary,
onPressed: onPressed,
onPressed: () {
removeModal!();
},
onLongPress: onLongPress,
onDoubleTap: onDoubleTap,
semanticsLabel: semanticsLabel,
Expand Down
19 changes: 19 additions & 0 deletions lib/src/widgets/gs_modal/gs_modal_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:gluestack_ui/gluestack_ui.dart';

class GSModalProvider extends InheritedWidget {
final VoidCallback removeModal;

const GSModalProvider({
required this.removeModal,
required Widget child,
}) : super(child: child);

@override
bool updateShouldNotify(GSModalProvider oldWidget) {
return removeModal != oldWidget.removeModal;
}

static GSModalProvider? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<GSModalProvider>();
}
}
7 changes: 0 additions & 7 deletions lib/src/widgets/gs_modal/gs_remove_modal.dart

This file was deleted.

0 comments on commit 27647db

Please sign in to comment.