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

Two new features proposed #194

Open
wants to merge 5 commits into
base: master
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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ android {
}

dependencies {
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
implementation 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.1'
}

1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.endigo.plugins.pdfviewflutter">
<!-- <io.endigo.plugins.pdfviewflutter android:id="@+id/pdfView" android:layout_width="0dp" android:layout_height="0dp" android:background="@color/gray_2"/> -->
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.view.View;
import android.net.Uri;

import android.graphics.Color;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -30,9 +30,12 @@ public class FlutterPDFView implements PlatformView, MethodCallHandler {

@SuppressWarnings("unchecked")
FlutterPDFView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {
System.out.println(params);
System.out.println("Hello, World!");

pdfView = new PDFView(context, null);
final boolean preventLinkNavigation = getBoolean(params, "preventLinkNavigation");

pdfView.setBackgroundColor(Color.parseColor(getString(params, "backgroundColor")));
methodChannel = new MethodChannel(messenger, "plugins.endigo.io/pdfview_" + id);
methodChannel.setMethodCallHandler(this);

Expand All @@ -49,6 +52,7 @@ else if (params.get("pdfData") != null) {
}

if (config != null) {

config
.enableSwipe(getBoolean(params, "enableSwipe"))
.swipeHorizontal(getBoolean(params, "swipeHorizontal"))
Expand All @@ -57,6 +61,10 @@ else if (params.get("pdfData") != null) {
.autoSpacing(getBoolean(params, "autoSpacing"))
.pageFling(getBoolean(params, "pageFling"))
.pageSnap(getBoolean(params, "pageSnap"))
.spacing(getInt(params,"spacingPx"))
// .setBackground(Color.valueOf(0xffff0000))
.enableAntialiasing(true)
// .onDraw(this)
.pageFitPolicy(getFitPolicy(params))
.enableAnnotationRendering(true)
.linkHandler(linkHandler).
Expand Down Expand Up @@ -100,7 +108,7 @@ public void onInitiallyRendered(int pages) {
public View getView() {
return pdfView;
}

@Override
public void onMethodCall(MethodCall methodCall, Result result) {
switch (methodCall.method) {
Expand Down
11 changes: 5 additions & 6 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/endigo/Projects/sdk/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/endigo/Projects/dart/flutter_pdfview/example"
export "FLUTTER_ROOT=/Users/theutsavg/Downloads/sdk/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/theutsavg/Documents/Projects/opensource/flutter_pdfview/example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=/Users/endigo/Projects/dart/flutter_pdfview/example/lib/main.dart"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1.0.0"
export "DART_DEFINES=RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ=="
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TRACK_WIDGET_CREATION=false"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=/Users/endigo/Projects/dart/flutter_pdfview/example/.dart_tool/package_config.json"
export "PACKAGE_CONFIG=.dart_tool/package_config.json"
54 changes: 38 additions & 16 deletions lib/flutter_pdfview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart' hide Widget;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/rendering.dart';
Expand All @@ -17,7 +18,7 @@ typedef LinkHandlerCallback = void Function(String? uri);
enum FitPolicy { WIDTH, HEIGHT, BOTH }

class PDFView extends StatefulWidget {
const PDFView({
PDFView({
Key? key,
this.filePath,
this.pdfData,
Expand All @@ -38,9 +39,14 @@ class PDFView extends StatefulWidget {
this.fitEachPage = true,
this.defaultPage = 0,
this.fitPolicy = FitPolicy.WIDTH,
this.pageSpacing = 8.0,
this.preventLinkNavigation = false,
})
: assert(filePath != null || pdfData != null),
this.backgroundColor = Colors.grey,
}) : assert(filePath != null || pdfData != null),
assert(
autoSpacing != false || pageSpacing != 0,
'Either use custom spacing or auto spacing',
),
super(key: key);

@override
Expand Down Expand Up @@ -80,19 +86,23 @@ class PDFView extends StatefulWidget {
final FitPolicy fitPolicy;
final bool fitEachPage;
final bool preventLinkNavigation;
final num pageSpacing;
final Color backgroundColor;
}

class _PDFViewState extends State<PDFView> {
final Completer<PDFViewController> _controller =
Completer<PDFViewController>();
Completer<PDFViewController>();

@override
Widget build(BuildContext context) {
if (defaultTargetPlatform == TargetPlatform.android) {
return PlatformViewLink(
viewType: 'plugins.endigo.io/pdfview',
surfaceFactory: (BuildContext context,
PlatformViewController controller,) {
surfaceFactory: (
BuildContext context,
PlatformViewController controller,
) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: widget.gestureRecognizers ??
Expand All @@ -108,9 +118,8 @@ class _PDFViewState extends State<PDFView> {
creationParams: _CreationParams.fromWidget(widget).toMap(),
creationParamsCodec: const StandardMessageCodec(),
)
..addOnPlatformViewCreatedListener(params
.onPlatformViewCreated)..addOnPlatformViewCreatedListener((
int id) {
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..addOnPlatformViewCreatedListener((int id) {
_onPlatformViewCreated(id);
})
..create();
Expand Down Expand Up @@ -141,7 +150,7 @@ class _PDFViewState extends State<PDFView> {
void didUpdateWidget(PDFView oldWidget) {
super.didUpdateWidget(oldWidget);
_controller.future.then(
(PDFViewController controller) => controller._updateWidget(widget));
(PDFViewController controller) => controller._updateWidget(widget));
}
}

Expand Down Expand Up @@ -178,17 +187,21 @@ class _CreationParams {
}

class _PDFViewSettings {
_PDFViewSettings({this.enableSwipe,
_PDFViewSettings({
this.enableSwipe,
this.swipeHorizontal,
this.password,
required this.pageSpacing,
this.nightMode,
this.autoSpacing,
this.pageFling,
this.pageSnap,
this.defaultPage,
this.fitPolicy,
this.fitEachPage,
this.preventLinkNavigation});
this.backgroundColor,
this.preventLinkNavigation,
});

static _PDFViewSettings fromWidget(PDFView widget) {
return _PDFViewSettings(
Expand All @@ -200,6 +213,9 @@ class _PDFViewSettings {
pageFling: widget.pageFling,
pageSnap: widget.pageSnap,
defaultPage: widget.defaultPage,
pageSpacing: widget.pageSpacing,
backgroundColor: widget.backgroundColor,
fitEachPage: widget.fitEachPage,
fitPolicy: widget.fitPolicy,
preventLinkNavigation: widget.preventLinkNavigation);
}
Expand All @@ -215,6 +231,8 @@ class _PDFViewSettings {
final FitPolicy? fitPolicy;
final bool? fitEachPage;
final bool? preventLinkNavigation;
final num pageSpacing;
final Color? backgroundColor;

Map<String, dynamic> toMap() {
return <String, dynamic>{
Expand All @@ -223,11 +241,14 @@ class _PDFViewSettings {
'password': password,
'nightMode': nightMode,
'autoSpacing': autoSpacing,
'spacingPx': pageSpacing.toInt(),
'pageFling': pageFling,
'pageSnap': pageSnap,
'defaultPage': defaultPage,
'fitPolicy': fitPolicy.toString(),
'fitEachPage': fitEachPage,
'backgroundColor':
'#${backgroundColor?.value.toRadixString(16).substring(2)}',
'preventLinkNavigation': preventLinkNavigation
};
}
Expand All @@ -251,9 +272,10 @@ class _PDFViewSettings {
}

class PDFViewController {
PDFViewController._(int id,
this._widget,)
: _channel = MethodChannel('plugins.endigo.io/pdfview_$id') {
PDFViewController._(
int id,
this._widget,
) : _channel = MethodChannel('plugins.endigo.io/pdfview_$id') {
_settings = _PDFViewSettings.fromWidget(_widget);
_channel.setMethodCallHandler(_onMethodCall);
}
Expand Down Expand Up @@ -314,7 +336,7 @@ class PDFViewController {

Future<bool?> setPage(int page) async {
final bool? isSet =
await _channel.invokeMethod('setPage', <String, dynamic>{
await _channel.invokeMethod('setPage', <String, dynamic>{
'page': page,
});
return isSet;
Expand Down