toImage method
Method to convert the SfRadialGauge as an image.
Returns the dart:ui.image
As this method is in the widget’s state class, you have to use a global key to access the state to call this method.
final GlobalKey<SfRadialGaugeState> _radialGaugeKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [SfRadialGauge(key: _radialGaugeKey),
RaisedButton(
child: Text(
'To Image',
),
onPressed: _renderImage,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
),
],
),
);
}
Future<void> _renderImage() async {
dart_ui.Image data = await
_radialGaugeKey.currentState.toImage(pixelRatio: 3.0);
final bytes = await data.toByteData(format: dart_ui.ImageByteFormat.png);
if (data != null) {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
color: Colors.white,
child: Image.memory(bytes.buffer.asUint8List()),
),
),
);
},
),
);
}
}
Implementation
/// Future<void> _renderImage() async {
/// dart_ui.Image data = await
/// _radialGaugeKey.currentState.toImage(pixelRatio: 3.0);
/// final bytes = await data.toByteData(format: dart_ui.ImageByteFormat.png);
/// if (data != null) {
/// await Navigator.of(context).push(
/// MaterialPageRoute(
/// builder: (BuildContext context) {
/// return Scaffold(
/// appBar: AppBar(),
/// body: Center(
/// child: Container(
/// color: Colors.white,
/// child: Image.memory(bytes.buffer.asUint8List()),
/// ),
/// ),
/// );
/// },
/// ),
/// );
/// }
/// }
/// ```
Future<dart_ui.Image> toImage({double pixelRatio = 1.0}) async {
final RenderRepaintBoundary? boundary =
context.findRenderObject() as RenderRepaintBoundary?;
final dart_ui.Image image = await boundary!.toImage(pixelRatio: pixelRatio);
return image;
}