Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
chore: create and preview pdf
- Loading branch information
1 parent
43fdc85
commit 9cc5070
Showing
7 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import 'dart:io'; | ||
import 'package:pdf/widgets.dart' as pdfLib; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:playground_flutter/configs/ioc.dart'; | ||
import 'package:playground_flutter/models/baseball.model.dart'; | ||
import 'package:playground_flutter/pages/code_examples/pdf_and_csv/pdf_viewer.dart'; | ||
import 'package:playground_flutter/services/sqlite_basebal_team.service.dart'; | ||
import 'package:playground_flutter/shared/widgets/crud_demo_list_item.widget.dart'; | ||
|
||
class PdfGeneratorDemo extends StatelessWidget { | ||
SqliteBaseballService _databaseService = Ioc.get<SqliteBaseballService>(); | ||
|
||
PdfGeneratorDemo({Key key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text('Pdf - generate and view'), | ||
actions: <Widget>[ | ||
IconButton( | ||
icon: Icon(Icons.picture_as_pdf), | ||
onPressed: () => _generatePdfAndView(context), | ||
), | ||
SizedBox(width: 10), | ||
], | ||
), | ||
body: StreamBuilder( | ||
stream: _databaseService.list(), | ||
builder: (_, AsyncSnapshot<List<BaseballModel>> snapshot) { | ||
if (!snapshot.hasData) { | ||
return Center( | ||
child: Text("No data found !!!"), | ||
); | ||
} | ||
|
||
return ListView( | ||
children: snapshot.data.map((item) { | ||
return CrudDemoListItem( | ||
item: item, | ||
onPressedDelete: (item) {}, | ||
onPressedEdit: (item) {}, | ||
); | ||
}).toList(), | ||
); | ||
}), | ||
); | ||
} | ||
|
||
_generatePdfAndView(context) async { | ||
List<BaseballModel> data = await _databaseService.list().first; | ||
final pdfLib.Document pdf = pdfLib.Document(deflate: zlib.encode); | ||
|
||
pdf.addPage( | ||
pdfLib.MultiPage( | ||
build: (context) => [ | ||
pdfLib.Table.fromTextArray(context: context, data: <List<String>>[ | ||
<String>['Name', 'Coach', 'players'], | ||
...data.map( | ||
(item) => [item.name, item.coach, item.players.toString()]) | ||
]), | ||
], | ||
), | ||
); | ||
|
||
final String dir = (await getApplicationDocumentsDirectory()).path; | ||
final String path = '$dir/baseball_teams.pdf'; | ||
final File file = File(path); | ||
file.writeAsBytesSync(pdf.save()); | ||
Navigator.of(context).push( | ||
MaterialPageRoute( | ||
builder: (_) => PdfViewerPage(path: path), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart'; | ||
|
||
class PdfViewerPage extends StatelessWidget { | ||
final String path; | ||
const PdfViewerPage({Key key, this.path}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return PDFViewerScaffold( | ||
path: path, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters