Closed joseph-grabinger closed 2 months ago
According to the docs:
[GlobalKeys] should usually be long-lived objects owned by a [State] object, for example
I minimized the example in 6af8fa7.
What I found out is the following:
import 'package:flutter/material.dart';
import 'package:flutter_to_pdf/flutter_to_pdf.dart';
void main() => runApp(const Demo());
class Demo extends StatefulWidget { const Demo({super.key});
@override
State
class _DemoState extends State
Future
@override Widget build(BuildContext context) => MaterialApp( theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, tabBarTheme: const TabBarTheme( labelColor: Colors.black87, ), ), home: Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.primary, title: const Text('Flutter to PDF - Demo'), ), bottomSheet: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ TextButton( onPressed: () async { final ExportOptions overrideOptions = ExportOptions( textFieldOptions: TextFieldOptions.uniform( interactive: false, ), checkboxOptions: CheckboxOptions.uniform( interactive: false, ), ); final pdf = await exportDelegate.exportToPdfDocument( 'Demo', overrideOptions: overrideOptions); saveFile(pdf, 'static-example'); }, child: const Row( children: [ Text('Export as static'), Icon(Icons.save_alt_outlined), ], ), ), TextButton( onPressed: () async { final pdf = await exportDelegate .exportToPdfDocument('Demo'); saveFile(pdf, 'interactive-example'); }, child: const Row( children: [ Text('Export as interactive'), Icon(Icons.save_alt_outlined), ], ), ), ], ), body: ExportFrame( frameId: 'Demo', exportDelegate: exportDelegate, child: CaptureWrapper( key: const Key('CaptureWraperKey'), child: Text("Hello World", key: GlobalKey()), ), ), ), ); }
However, extracting everything inside the `CaptureWrapper` into a standalone `StatefulWidget` makes it work (not completely sure why since `Demo ` is also a `StatefulWidget`):
```dart
import 'package:flutter/material.dart';
import 'package:flutter_to_pdf/flutter_to_pdf.dart';
void main() => runApp(const Demo());
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
final ExportDelegate exportDelegate = ExportDelegate();
Future<void> saveFile(document, String name) async {...}
@override
Widget build(BuildContext context) => MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
tabBarTheme: const TabBarTheme(
labelColor: Colors.black87,
),
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.primary,
title: const Text('Flutter to PDF - Demo'),
),
bottomSheet: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
onPressed: () async {
final ExportOptions overrideOptions = ExportOptions(
textFieldOptions: TextFieldOptions.uniform(
interactive: false,
),
checkboxOptions: CheckboxOptions.uniform(
interactive: false,
),
);
final pdf = await exportDelegate.exportToPdfDocument(
'Demo',
overrideOptions: overrideOptions);
saveFile(pdf, 'static-example');
},
child: const Row(
children: [
Text('Export as static'),
Icon(Icons.save_alt_outlined),
],
),
),
TextButton(
onPressed: () async {
final pdf = await exportDelegate
.exportToPdfDocument('Demo');
saveFile(pdf, 'interactive-example');
},
child: const Row(
children: [
Text('Export as interactive'),
Icon(Icons.save_alt_outlined),
],
),
),
],
),
body: ExportFrame(
frameId: 'Demo',
exportDelegate: exportDelegate,
child: const Body(),
),
),
);
}
class Body extends StatefulWidget {
const Body({super.key});
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final globalKey = GlobalKey();
@override
Widget build(BuildContext context) {
return CaptureWrapper(
key: const Key('CaptureWraperKey'),
child: Text("Hello World", key: globalKey),
);
}
}
Hey @Vera-Spoettl, this example works for me (ran on macOS) with Flutter version
3.22.3
.Please run the example and switch to the "Charts & Custom Paint" tab, where I replaced the CustomPaint example with a simple
Text
widget using aGlobalKey
.Here my flutter doctor: