Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new close customization widget option. #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .fvm/flutter_sdk
4 changes: 4 additions & 0 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.13.6",
"flavors": {}
}
32 changes: 32 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,20 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: () {
showImageViewer(
context,

Image.network("https://picsum.photos/id/1001/4912/3264")
.image,
closeWidget: Container(
clipBehavior: Clip.hardEdge,
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red.withOpacity(0.5),
shape: BoxShape.circle
),
child: const Icon(Icons.close, color: Colors.white,),
),

swipeDismissible: true,
doubleTapZoomable: true);
}),
Expand All @@ -72,6 +84,16 @@ class _MyHomePageState extends State<MyHomePage> {
MultiImageProvider multiImageProvider =
MultiImageProvider(_imageProviders);
showImageViewerPager(context, multiImageProvider,
closeWidget: Container(
clipBehavior: Clip.hardEdge,
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red.withOpacity(0.5),
shape: BoxShape.circle
),
child: const Icon(Icons.close, color: Colors.white,),
),
swipeDismissible: true, doubleTapZoomable: true);
}),
ElevatedButton(
Expand All @@ -86,6 +108,16 @@ class _MyHomePageState extends State<MyHomePage> {
].toList(),
initialIndex: 2);
showImageViewerPager(context, customImageProvider,
closeWidget: Container(
clipBehavior: Clip.hardEdge,
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red.withOpacity(0.5),
shape: BoxShape.circle
),
child: const Icon(Icons.close, color: Colors.white,),
),
onPageChanged: (page) {
// print("Page changed to $page");
}, onViewerDismissed: (page) {
Expand Down
6 changes: 5 additions & 1 deletion lib/easy_image_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Future<Dialog?> showImageViewer(
bool useSafeArea = false,
bool swipeDismissible = false,
bool doubleTapZoomable = false,
Widget? closeWidget,
Color backgroundColor = _defaultBackgroundColor,
String closeButtonTooltip = _defaultCloseButtonTooltip,
Color closeButtonColor = _defaultCloseButtonColor}) {
Expand All @@ -49,6 +50,7 @@ Future<Dialog?> showImageViewer(
swipeDismissible: swipeDismissible,
doubleTapZoomable: doubleTapZoomable,
backgroundColor: backgroundColor,
closeWidget: closeWidget,
closeButtonTooltip: closeButtonTooltip,
closeButtonColor: closeButtonColor);
}
Expand All @@ -74,6 +76,7 @@ Future<Dialog?> showImageViewerPager(
bool useSafeArea = false,
bool swipeDismissible = false,
bool doubleTapZoomable = false,
Widget? closeWidget,
Color backgroundColor = _defaultBackgroundColor,
String closeButtonTooltip = _defaultCloseButtonTooltip,
Color closeButtonColor = _defaultCloseButtonColor}) {
Expand All @@ -95,6 +98,7 @@ Future<Dialog?> showImageViewerPager(
doubleTapZoomable: doubleTapZoomable,
backgroundColor: backgroundColor,
closeButtonColor: closeButtonColor,
closeButtonTooltip: closeButtonTooltip);
closeWidget: closeWidget,
closeButtonTooltip: closeButtonTooltip,);
});
}
74 changes: 38 additions & 36 deletions lib/src/easy_image_viewer_dismissible_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class EasyImageViewerDismissibleDialog extends StatefulWidget {
final Color backgroundColor;
final String closeButtonTooltip;
final Color closeButtonColor;
final Widget? closeWidget;

/// Refer to [showImageViewerPager] for the arguments
const EasyImageViewerDismissibleDialog(this.imageProvider,
Expand All @@ -29,18 +30,17 @@ class EasyImageViewerDismissibleDialog extends StatefulWidget {
this.useSafeArea = false,
this.swipeDismissible = false,
this.doubleTapZoomable = false,
this.closeWidget,
required this.backgroundColor,
required this.closeButtonTooltip,
required this.closeButtonColor})
: super(key: key);

@override
State<EasyImageViewerDismissibleDialog> createState() =>
_EasyImageViewerDismissibleDialogState();
State<EasyImageViewerDismissibleDialog> createState() => _EasyImageViewerDismissibleDialogState();
}

class _EasyImageViewerDismissibleDialogState
extends State<EasyImageViewerDismissibleDialog> {
class _EasyImageViewerDismissibleDialogState extends State<EasyImageViewerDismissibleDialog> {
/// This is used to either activate or deactivate the ability to swipe-to-dismissed, based on
/// whether the current image is zoomed in (scale > 0) or not.
DismissDirection _dismissDirection = DismissDirection.down;
Expand All @@ -56,8 +56,7 @@ class _EasyImageViewerDismissibleDialogState
@override
void initState() {
super.initState();
_pageController =
PageController(initialPage: widget.imageProvider.initialIndex);
_pageController = PageController(initialPage: widget.imageProvider.initialIndex);
if (widget.onPageChanged != null) {
_internalPageChangeListener = () {
widget.onPageChanged!(_pageController.page?.round() ?? 0);
Expand Down Expand Up @@ -88,36 +87,39 @@ class _EasyImageViewerDismissibleDialogState
insetPadding: const EdgeInsets.all(0),
// We set the shape here to ensure no rounded corners allow any of the
// underlying view to show. We want the whole background to be covered.
shape:
const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
child: Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: <Widget>[
EasyImageViewPager(
easyImageProvider: widget.imageProvider,
pageController: _pageController,
doubleTapZoomable: widget.doubleTapZoomable,
onScaleChanged: (scale) {
setState(() {
_dismissDirection = scale <= 1.0
? DismissDirection.down
: DismissDirection.none;
});
}),
Positioned(
top: 5,
right: 5,
child: IconButton(
icon: const Icon(Icons.close),
color: widget.closeButtonColor,
tooltip: widget.closeButtonTooltip,
onPressed: () {
Navigator.of(context).pop();
_handleDismissal();
},
))
])));
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
child: Stack(clipBehavior: Clip.none, alignment: Alignment.center, children: <Widget>[
EasyImageViewPager(
easyImageProvider: widget.imageProvider,
pageController: _pageController,
doubleTapZoomable: widget.doubleTapZoomable,
onScaleChanged: (scale) {
setState(() {
_dismissDirection = scale <= 1.0 ? DismissDirection.down : DismissDirection.none;
});
}),
Positioned(
top: 5,
right: 5,
child: widget.closeWidget == null
? IconButton(
icon: const Icon(Icons.close),
color: widget.closeButtonColor,
tooltip: widget.closeButtonTooltip,
onPressed: () {
Navigator.of(context).pop();
_handleDismissal();
},
)
: GestureDetector(

onTap: () {
Navigator.of(context).pop();
_handleDismissal();
},
child: widget.closeWidget!,
))
])));

if (widget.swipeDismissible) {
return Dismissible(
Expand Down