content: import 'dart:math';
  import 'dart:typed_data';
  import 'dart:ui' as ui;
  import 'dart:ui';
  
  import 'package:flutter/material.dart';
  
  void main() => runApp(App());
  
  const kCanvasSize = 200.0;
  
  class App extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          body: ImageGenerator(),
        ),
        debugShowCheckedModeBanner: false,
      );
    }
  }
  
  class ImageGenerator extends StatefulWidget {
    final Random rd;
    final int numColors;
  
    ImageGenerator()
        : rd = Random(),
          numColors = Colors.primaries.length;
  
    @override
    _ImageGeneratorState createState() => _ImageGeneratorState();
  }
  
  class _ImageGeneratorState extends State<ImageGenerator> {
    ByteData imgBytes;
  
    @override
    Widget build(BuildContext context) {
      return Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: RaisedButton(
                  child: Text('Generate image'), onPressed: generateImage),
            ),
            imgBytes != null
                ? Center(
                    child: Image.memory(
                    Uint8List.view(imgBytes.buffer),
                    width: kCanvasSize,
                    height: kCanvasSize,
                  ))
                : Container()
          ],
        ),
      );
    }
  
    void generateImage() async {
      final color = Colors.primaries[widget.rd.nextInt(widget.numColors)];
  
      final recorder = ui.PictureRecorder();
      final canvas = Canvas(recorder,
          Rect.fromPoints(Offset(0.0, 0.0), Offset(kCanvasSize, kCanvasSize)));
  
      final stroke = Paint()
        ..color = Colors.grey
        ..style = PaintingStyle.stroke;
  
      canvas.drawRect(Rect.fromLTWH(0.0, 0.0, kCanvasSize, kCanvasSize), stroke);
  
      final paint = Paint()
        ..color = color
        ..style = PaintingStyle.fill;
  
      canvas.drawCircle(
          Offset(
            widget.rd.nextDouble() * kCanvasSize,
            widget.rd.nextDouble() * kCanvasSize,
          ),
          20.0,
          paint);
  
      final picture = recorder.endRecording();
      final img = await picture.toImage(200, 200);
      final pngBytes = await img.toByteData(format: ImageByteFormat.png);
  
      setState(() {
        imgBytes = pngBytes;
      });
    }
  }
  