David PHAM-VAN

Fix dispose state issue

1 # Changelog 1 # Changelog
2 2
  3 +## 5.7.2
  4 +
  5 +- Fix dispose state issue
  6 +
3 ## 5.7.1 7 ## 5.7.1
4 8
5 - Update Google Fonts, fixes documentation issues 9 - Update Google Fonts, fixes documentation issues
@@ -237,7 +237,26 @@ class PdfPageFormatAction extends StatelessWidget { @@ -237,7 +237,26 @@ class PdfPageFormatAction extends StatelessWidget {
237 final theme = Theme.of(context); 237 final theme = Theme.of(context);
238 final iconColor = theme.primaryIconTheme.color ?? Colors.white; 238 final iconColor = theme.primaryIconTheme.color ?? Colors.white;
239 final data = PdfPreviewController.listen(context); 239 final data = PdfPreviewController.listen(context);
240 - final keys = pageFormats.keys.toList(); 240 + final _pageFormats = <String, PdfPageFormat>{...pageFormats};
  241 +
  242 + var format = data.pageFormat;
  243 + final orientation = data.horizontal;
  244 +
  245 + if (!_pageFormats.values.contains(data.pageFormat)) {
  246 + var found = false;
  247 + for (final f in _pageFormats.values) {
  248 + if (format.portrait == f.portrait) {
  249 + format = f;
  250 + found = true;
  251 + break;
  252 + }
  253 + }
  254 + if (!found) {
  255 + _pageFormats['---'] = format;
  256 + }
  257 + }
  258 +
  259 + final keys = _pageFormats.keys.toList()..sort();
241 260
242 return DropdownButton<PdfPageFormat>( 261 return DropdownButton<PdfPageFormat>(
243 dropdownColor: theme.primaryColor, 262 dropdownColor: theme.primaryColor,
@@ -245,12 +264,12 @@ class PdfPageFormatAction extends StatelessWidget { @@ -245,12 +264,12 @@ class PdfPageFormatAction extends StatelessWidget {
245 Icons.arrow_drop_down, 264 Icons.arrow_drop_down,
246 color: iconColor, 265 color: iconColor,
247 ), 266 ),
248 - value: data.pageFormat, 267 + value: format,
249 items: List<DropdownMenuItem<PdfPageFormat>>.generate( 268 items: List<DropdownMenuItem<PdfPageFormat>>.generate(
250 - pageFormats.length, 269 + _pageFormats.length,
251 (int index) { 270 (int index) {
252 final key = keys[index]; 271 final key = keys[index];
253 - final val = pageFormats[key]; 272 + final val = _pageFormats[key]!;
254 return DropdownMenuItem<PdfPageFormat>( 273 return DropdownMenuItem<PdfPageFormat>(
255 value: val, 274 value: val,
256 child: Text(key, style: TextStyle(color: iconColor)), 275 child: Text(key, style: TextStyle(color: iconColor)),
@@ -259,7 +278,8 @@ class PdfPageFormatAction extends StatelessWidget { @@ -259,7 +278,8 @@ class PdfPageFormatAction extends StatelessWidget {
259 ), 278 ),
260 onChanged: (PdfPageFormat? pageFormat) { 279 onChanged: (PdfPageFormat? pageFormat) {
261 if (pageFormat != null) { 280 if (pageFormat != null) {
262 - data.pageFormat = pageFormat; 281 + data.pageFormat =
  282 + orientation ? pageFormat.landscape : pageFormat.portrait;
263 } 283 }
264 }, 284 },
265 ); 285 );
@@ -19,41 +19,52 @@ import 'package:pdf/pdf.dart'; @@ -19,41 +19,52 @@ import 'package:pdf/pdf.dart';
19 19
20 import '../callback.dart'; 20 import '../callback.dart';
21 21
22 -mixin PdfPreviewData implements ChangeNotifier {  
23 - // PdfPreviewData(this.build); 22 +typedef ComputePageFormat = PdfPageFormat Function();
  23 +
  24 +class PdfPreviewData extends ChangeNotifier {
  25 + PdfPreviewData({
  26 + PdfPageFormat? initialPageFormat,
  27 + required this.buildDocument,
  28 + required Map<String, PdfPageFormat> pageFormats,
  29 + required ComputePageFormat onComputeActualPageFormat,
  30 + }) : assert(pageFormats.isNotEmpty),
  31 + _onComputeActualPageFormat = onComputeActualPageFormat {
  32 + _pageFormat = initialPageFormat ??
  33 + (pageFormats[localPageFormat] ?? pageFormats.values.first);
  34 + }
24 35
25 - LayoutCallback get buildDocument; 36 + late PdfPageFormat _pageFormat;
26 37
27 - PdfPageFormat? _pageFormat; 38 + final LayoutCallback buildDocument;
28 39
29 - PdfPageFormat get initialPageFormat; 40 + final ComputePageFormat _onComputeActualPageFormat;
30 41
31 - PdfPageFormat get pageFormat => _pageFormat ?? initialPageFormat; 42 + PdfPageFormat get pageFormat => _pageFormat;
32 43
33 set pageFormat(PdfPageFormat value) { 44 set pageFormat(PdfPageFormat value) {
34 - if (_pageFormat == value) {  
35 - return; 45 + if (_pageFormat != value) {
  46 + _pageFormat = value;
  47 + notifyListeners();
36 } 48 }
37 - _pageFormat = value;  
38 - notifyListeners();  
39 } 49 }
40 50
41 - bool? _horizontal;  
42 -  
43 /// Is the print horizontal 51 /// Is the print horizontal
44 - bool get horizontal => _horizontal ?? pageFormat.width > pageFormat.height; 52 + bool get horizontal => _pageFormat.width > _pageFormat.height;
45 53
46 set horizontal(bool value) { 54 set horizontal(bool value) {
47 - if (_horizontal == value) {  
48 - return; 55 + final format = value ? _pageFormat.landscape : _pageFormat.portrait;
  56 + if (format != _pageFormat) {
  57 + _pageFormat = format;
  58 + notifyListeners();
49 } 59 }
50 - _horizontal = value;  
51 - notifyListeners();  
52 } 60 }
53 61
54 /// Computed page format 62 /// Computed page format
55 - PdfPageFormat get computedPageFormat =>  
56 - horizontal ? pageFormat.landscape : pageFormat.portrait; 63 + @Deprecated('Use pageFormat instead')
  64 + PdfPageFormat get computedPageFormat => _pageFormat;
  65 +
  66 + /// The page format of the document
  67 + PdfPageFormat get actualPageFormat => _onComputeActualPageFormat();
57 68
58 String get localPageFormat { 69 String get localPageFormat {
59 final locale = WidgetsBinding.instance!.window.locale; 70 final locale = WidgetsBinding.instance!.window.locale;
@@ -65,8 +76,6 @@ mixin PdfPreviewData implements ChangeNotifier { @@ -65,8 +76,6 @@ mixin PdfPreviewData implements ChangeNotifier {
65 } 76 }
66 return 'A4'; 77 return 'A4';
67 } 78 }
68 -  
69 - PdfPageFormat get actualPageFormat => pageFormat;  
70 } 79 }
71 80
72 class PdfPreviewController extends InheritedNotifier { 81 class PdfPreviewController extends InheritedNotifier {
@@ -158,41 +158,17 @@ class PdfPreview extends StatefulWidget { @@ -158,41 +158,17 @@ class PdfPreview extends StatefulWidget {
158 _PdfPreviewState createState() => _PdfPreviewState(); 158 _PdfPreviewState createState() => _PdfPreviewState();
159 } 159 }
160 160
161 -class _PdfPreviewState extends State<PdfPreview>  
162 - with PdfPreviewData, ChangeNotifier { 161 +class _PdfPreviewState extends State<PdfPreview> {
163 final previewWidget = GlobalKey<PdfPreviewCustomState>(); 162 final previewWidget = GlobalKey<PdfPreviewCustomState>();
  163 + late PdfPreviewData previewData;
164 164
165 /// Printing subsystem information 165 /// Printing subsystem information
166 PrintingInfo? info; 166 PrintingInfo? info;
167 - var infoLoaded = false;  
168 -  
169 - @override  
170 - LayoutCallback get buildDocument => widget.build;  
171 -  
172 - @override  
173 - PdfPageFormat get initialPageFormat =>  
174 - widget.initialPageFormat ??  
175 - (widget.pageFormats.isNotEmpty  
176 - ? (widget.pageFormats[localPageFormat] ??  
177 - widget.pageFormats.values.first)  
178 - : (PdfPreview._defaultPageFormats[localPageFormat]) ??  
179 - PdfPreview._defaultPageFormats.values.first);  
180 -  
181 - @override  
182 - PdfPageFormat get pageFormat {  
183 - var _pageFormat = super.pageFormat;  
184 -  
185 - if (!widget.pageFormats.containsValue(_pageFormat)) {  
186 - _pageFormat = initialPageFormat;  
187 - pageFormat = _pageFormat;  
188 - }  
189 167
190 - return _pageFormat;  
191 - } 168 + var infoLoaded = false;
192 169
193 - @override  
194 - PdfPageFormat get actualPageFormat {  
195 - var format = pageFormat; 170 + PdfPageFormat computeActualPageFormat() {
  171 + var format = previewData.pageFormat;
196 final pages = previewWidget.currentState?.pages ?? const []; 172 final pages = previewWidget.currentState?.pages ?? const [];
197 final dpi = previewWidget.currentState?.dpi ?? 72; 173 final dpi = previewWidget.currentState?.dpi ?? 72;
198 174
@@ -209,7 +185,16 @@ class _PdfPreviewState extends State<PdfPreview> @@ -209,7 +185,16 @@ class _PdfPreviewState extends State<PdfPreview>
209 185
210 @override 186 @override
211 void initState() { 187 void initState() {
212 - addListener(() { 188 + previewData = PdfPreviewData(
  189 + buildDocument: widget.build,
  190 + pageFormats: widget.pageFormats.isNotEmpty
  191 + ? widget.pageFormats
  192 + : PdfPreview._defaultPageFormats,
  193 + initialPageFormat: widget.initialPageFormat,
  194 + onComputeActualPageFormat: computeActualPageFormat,
  195 + );
  196 +
  197 + previewData.addListener(() {
213 if (mounted) { 198 if (mounted) {
214 setState(() {}); 199 setState(() {});
215 } 200 }
@@ -219,10 +204,24 @@ class _PdfPreviewState extends State<PdfPreview> @@ -219,10 +204,24 @@ class _PdfPreviewState extends State<PdfPreview>
219 } 204 }
220 205
221 @override 206 @override
  207 + void dispose() {
  208 + previewData.dispose();
  209 + super.dispose();
  210 + }
  211 +
  212 + @override
222 void didUpdateWidget(covariant PdfPreview oldWidget) { 213 void didUpdateWidget(covariant PdfPreview oldWidget) {
223 if (oldWidget.build != widget.build || 214 if (oldWidget.build != widget.build ||
224 widget.shouldRepaint || 215 widget.shouldRepaint ||
225 widget.pageFormats != oldWidget.pageFormats) { 216 widget.pageFormats != oldWidget.pageFormats) {
  217 + previewData = PdfPreviewData(
  218 + buildDocument: widget.build,
  219 + pageFormats: widget.pageFormats.isNotEmpty
  220 + ? widget.pageFormats
  221 + : PdfPreview._defaultPageFormats,
  222 + initialPageFormat: previewData.pageFormat,
  223 + onComputeActualPageFormat: computeActualPageFormat,
  224 + );
226 setState(() {}); 225 setState(() {});
227 } 226 }
228 super.didUpdateWidget(oldWidget); 227 super.didUpdateWidget(oldWidget);
@@ -301,7 +300,7 @@ class _PdfPreviewState extends State<PdfPreview> @@ -301,7 +300,7 @@ class _PdfPreviewState extends State<PdfPreview>
301 }()); 300 }());
302 301
303 return PdfPreviewController( 302 return PdfPreviewController(
304 - data: this, 303 + data: previewData,
305 child: Column( 304 child: Column(
306 mainAxisAlignment: MainAxisAlignment.center, 305 mainAxisAlignment: MainAxisAlignment.center,
307 children: <Widget>[ 306 children: <Widget>[
@@ -313,7 +312,7 @@ class _PdfPreviewState extends State<PdfPreview> @@ -313,7 +312,7 @@ class _PdfPreviewState extends State<PdfPreview>
313 maxPageWidth: widget.maxPageWidth, 312 maxPageWidth: widget.maxPageWidth,
314 onError: widget.onError, 313 onError: widget.onError,
315 padding: widget.padding, 314 padding: widget.padding,
316 - pageFormat: computedPageFormat, 315 + pageFormat: previewData.pageFormat,
317 pages: widget.pages, 316 pages: widget.pages,
318 pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, 317 pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration,
319 previewPageMargin: widget.previewPageMargin, 318 previewPageMargin: widget.previewPageMargin,
@@ -6,7 +6,7 @@ description: > @@ -6,7 +6,7 @@ description: >
6 homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing 6 homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
7 repository: https://github.com/DavBfr/dart_pdf 7 repository: https://github.com/DavBfr/dart_pdf
8 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 8 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
9 -version: 5.7.1 9 +version: 5.7.2
10 10
11 environment: 11 environment:
12 sdk: ">=2.12.0 <3.0.0" 12 sdk: ">=2.12.0 <3.0.0"