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 easy_image_viewer with page.open_image_viewer([img_paths]) method #943

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 5 additions & 0 deletions package/lib/src/reducers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'protocol/update_control_props_payload.dart';
import 'utils/client_storage.dart';
import 'utils/desktop.dart';
import 'utils/launch_url.dart';
import 'utils/image_viewer.dart';
import 'utils/platform_utils_non_web.dart'
if (dart.library.js) "utils/platform_utils_web.dart";
import 'utils/session_store_non_web.dart'
Expand Down Expand Up @@ -211,6 +212,10 @@ AppState appReducer(AppState state, dynamic action) {
methodId: action.payload.methodId,
result: value.toString()))));
break;
case "openImageViewer":
openImageViewer(action.payload.args["image"]!, action.payload.args["swipe_dismissable"]?.toLowerCase() == "true", action.payload.args["double_tap_zoomable"]?.toLowerCase() == "true", action.payload.args["background_color"], action.payload.args["close_button_color"], action.payload.args["close_button_tooltip"], action.payload.args["immersive"]?.toLowerCase() == "true",
int.tryParse(action.payload.args["initial_index"] ?? "0"));
break;
case "windowToFront":
windowToFront();
break;
Expand Down
51 changes: 51 additions & 0 deletions package/lib/src/utils/image_viewer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:easy_image_viewer/easy_image_viewer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../utils/colors.dart';
import '../utils/images.dart';
import '../models/app_state.dart';
import '../models/page_args_model.dart';

/// Shows the given [imageProvider] in a full-screen [Dialog].
/// The optional [swipeDismissible] boolean defaults to false and allows swipe-down-to-dismiss.
/// The optional [doubleTapZoomable] boolean defaults to false and allows double tap to zoom.
/// The [backgroundColor] defaults to black, but can be set to any other color.
/// The [closeButtonColor] defaults to white, but can be set to any other color.
/// The [closeButtonTooltip] text is displayed when the user long-presses on the close button.
/// Setting [immersive] to false will prevent the top and bottom bars from being hidden.
/// The [initialIndex] is the image to start with in a MultiPage viewer.
Future openImageViewer(String image, bool swipeDismissible, bool doubleTapZoomable, String? backgroundColor, String? closeButtonColor, String? closeButtonTooltip, bool? immersive, int? initialIndex) async {
return StoreConnector<AppState, PageArgsModel>(
distinct: true,
converter: (store) => PageArgsModel.fromStore(store),
builder: (context, pageArgs) {
var imageProvider;
if (image.contains('|')) {
List<ImageProvider<Object>> imageList = [];
for (String imageSrc in image.split('|')) {
var assetSrc = getAssetSrc(imageSrc, pageArgs.pageUri!, pageArgs.assetsDir);
imageList.add(assetSrc.isFile ? getFileImageProvider(assetSrc.path) : NetworkImage(assetSrc.path));
}
imageProvider = MultiImageProvider(imageList, initialIndex: initialIndex ?? 0);
} else {
var assetSrc = getAssetSrc(image, pageArgs.pageUri!, pageArgs.assetsDir);
imageProvider = SingleImageProvider(assetSrc.isFile ? getFileImageProvider(assetSrc.path) : NetworkImage(assetSrc.path));
}
var background_color = checkString(backgroundColor) ? HexColor.fromString(Theme.of(context), backgroundColor!) : Colors.black;
var close_button_color = checkString(closeButtonColor) ? HexColor.fromString(Theme.of(context), closeButtonColor!) : Colors.white;
showImageViewerPager(
context,
imageProvider!,
swipeDismissible: swipeDismissible,
doubleTapZoomable: doubleTapZoomable,
backgroundColor: background_color!,
closeButtonColor: close_button_color!,
closeButtonTooltip: closeButtonTooltip ?? "Close",
immersive: immersive ?? true,
);
return Container();
},
);
}

bool checkString(value) => !(['', null, 0, false].contains(value));
1 change: 1 addition & 0 deletions package/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies:
window_to_front: ^0.0.3
audioplayers: ^3.0.0
shake: ^2.1.0
easy_image_viewer: ^1.2.0
path: ^1.8.2

dev_dependencies:
Expand Down
49 changes: 49 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,55 @@ async def can_launch_url_async(self, url: str):
== "true"
)

def open_image_viewer(
self,
image: Union[str, List[str]],
swipe_dismissible: Optional[bool] = True,
double_tap_zoomable: Optional[bool] = True,
background_color: Optional[str] = None,
close_button_color: Optional[str] = None,
close_button_tooltip: Optional[str] = "Close",
immersive: Optional[bool] = True,
initial_index: Optional[int] = 0,
):
self.invoke_method(
"openImageViewer",
self.__get_image_viewer_args(image=image,
swipe_dismissible=swipe_dismissible,
double_tap_zoomable=double_tap_zoomable,
background_color=background_color,
close_button_color=close_button_color,
close_button_tooltip=close_button_tooltip,
immersive=immersive,
initial_index=initial_index,
),
)

def __get_image_viewer_args(
self,
image: Union[str, List[str]],
swipe_dismissible: Optional[bool] = True,
double_tap_zoomable: Optional[bool] = True,
background_color: Optional[str] = None,
close_button_color: Optional[str] = None,
close_button_tooltip: Optional[str] = "Close",
immersive: Optional[bool] = True,
initial_index: Optional[int] = 0,
):
img = image
if isinstance(image, List):
img = '|'.join(image)
args = {"image": img,
"swipe_dismissible":str(swipe_dismissible),
"double_tap_zoomable":str(double_tap_zoomable),
"background_color":str(background_color),
"close_button_color":str(close_button_color),
"close_button_tooltip":close_button_tooltip,
"immersive":str(immersive),
"initial_index":str(initial_index),
};
return args

def close_in_app_web_view(self):
self.invoke_method("closeInAppWebView")

Expand Down