toImage method

Future<Image> toImage({
  1. double pixelRatio = 1.0,
})

Method to convert the SfPyramidChart 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<SfPyramidChartState> _key = GlobalKey();
@override
Widget build(BuildContext context) {
 return Scaffold(
   body: Column(
     children: [SfPyramidChart(
       key: _key
           series: PyramidSeries<_PyramidData, String>(
                         dataSource: data,
                         xValueMapper: (_PyramidData data, _) => data.xData,
                         yValueMapper: (_PyramidData data, _) => data.yData)
       ),
            ],
       ),
             RaisedButton(
               child: Text(
                 'To Image',
               ),
              onPressed: _renderImage,
               shape: RoundedRectangleBorder(
                   borderRadius: BorderRadius.circular(20)),
             )
     ],
   ),
 );
}
Future<void> _renderImage() async {
 dart_ui.Image data = await _key.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 _key.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; //get the render object from context
  final dart_ui.Image image =
      await boundary.toImage(pixelRatio: pixelRatio); // Convert
  // the repaint boundary as image
  return image;
}