Skip to content

Commit 6cbf84c

Browse files
authored
Merge pull request #3776 from syncfusion-content/956054-hotfix
956054: Include how to pass pdf document in custom widget on flutterflow
2 parents b5e8f21 + 4ae9bd7 commit 6cbf84c

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

Document-Processing/PDF/PDF-Viewer/flutter/How-to/custom-widget-on-flutterflow.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,134 @@ Future importPdfjsScript() async {
109109

110110
![main.dart](images/add-custom-action.png)
111111

112+
### Pass PDF Document to the Custom Widget
113+
114+
You can pass the PDF document to the custom widget as a parameter. The following steps explain how to pass the PDF document URL to the custom widget.
115+
116+
1. In the `Custom Widget Settings` panel on the right, click the `+ Add Parameters` button to add a new parameter.
117+
118+
![Add Parameter](images/add-parameter.png)
119+
120+
2. Set the parameter `Name` as `url`, choose the `Type` as `String`, and enable the `Nullable` option.
121+
122+
![Add url Parameter](images/add-url-parameter.png)
123+
124+
3. Modify the `PDFViewerWidget` class constructor to include the newly added `url` parameter, and declare the `url` field as shown below.
125+
126+
{% tabs %}
127+
{% highlight Dart %}
128+
129+
class PDFViewerWidget extends StatefulWidget {
130+
const PDFViewerWidget(
131+
{super.key, this.width, this.height, required this.url});
132+
133+
final double? width;
134+
final double? height;
135+
final String url;
136+
137+
@override
138+
State<PDFViewerWidget> createState() => _PDFViewerWidgetState();
139+
}
140+
141+
{% endhighlight %}
142+
{% endtabs %}
143+
144+
![Modify Widget Code](images/modify-widget-code.png)
145+
146+
4. Replace the constant URL in the `SfPdfViewer.network` constructor with the `url` parameter that is passed to the widget.
147+
148+
{% tabs %}
149+
{% highlight Dart hl_lines="18 19" %}
150+
151+
@override
152+
Widget build(BuildContext context) {
153+
return Scaffold(
154+
appBar: AppBar(
155+
title: const Text('Flutter PDF Viewer'),
156+
actions: <Widget>[
157+
IconButton(
158+
icon: const Icon(
159+
Icons.bookmark,
160+
color: Colors.white,
161+
),
162+
onPressed: () {
163+
_pdfViewerKey.currentState?.openBookmarkView();
164+
},
165+
),
166+
],
167+
),
168+
body: SfPdfViewer.network(
169+
widget.url,
170+
key: _pdfViewerKey,
171+
),
172+
);
173+
}
174+
175+
{% endhighlight %}
176+
{% endtabs %}
177+
178+
5. Save the changes and click the `Compile Code` button to compile the custom code.
179+
180+
6. On the canvas, select the `PDFViewerWidget` and enter the PDF document URL in the `url` field of the `Custom Widget Properties` section.
181+
182+
![Set url in Widget Properties](images/set-url-in-widget.png)
183+
184+
In this example, we pass `url` as a parameter and use the `SfPdfViewer.network` constructor to display the PDF document. Similarly, you can use the `SfPdfViewer.file` or `SfPdfViewer.memory` constructor to display a PDF document of your choice.
185+
186+
>**Note**: To display a PDF document from an **Uploaded File (Bytes)** in FlutterFlow, add a parameter of type `FFUploadedFile` (you can find this exact type by clicking the `View Boilerplate Code` button) and then use the `SfPdfViewer.memory` constructor with the uploaded file bytes. See the code snippet below.
187+
188+
{% tabs %}
189+
{% highlight Dart hl_lines="11 37 38" %}
190+
191+
class PDFViewerWidget extends StatefulWidget {
192+
const PDFViewerWidget({
193+
super.key,
194+
this.width,
195+
this.height,
196+
this.file,
197+
});
198+
199+
final double? width;
200+
final double? height;
201+
final FFUploadedFile? file;
202+
203+
@override
204+
State<PDFViewerWidget> createState() => _PDFViewerWidgetState();
205+
}
206+
207+
class _PDFViewerWidgetState extends State<PDFViewerWidget> {
208+
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
209+
210+
@override
211+
Widget build(BuildContext context) {
212+
return widget.file != null && widget.file!.bytes != null
213+
? Scaffold(
214+
appBar: AppBar(
215+
title: Text('Flutter PDF Viewer'),
216+
actions: <Widget>[
217+
IconButton(
218+
icon: Icon(
219+
Icons.bookmark,
220+
color: Colors.white,
221+
),
222+
onPressed: () {
223+
_pdfViewerKey.currentState?.openBookmarkView();
224+
},
225+
),
226+
],
227+
),
228+
body: SfPdfViewer.memory(
229+
widget.file!.bytes!,
230+
key: _pdfViewerKey,
231+
),
232+
)
233+
: Container();
234+
}
235+
}
236+
237+
{% endhighlight %}
238+
{% endtabs %}
239+
112240
### Utilizing the Custom Widget
113241

114242
1. Navigate to the `Widget Palette` located in the left-side navigation menu.
74.4 KB
Loading
74 KB
Loading
24 KB
Loading
57 KB
Loading

0 commit comments

Comments
 (0)