Home > Back-end >  How to Make image from flutter material widget on server side
How to Make image from flutter material widget on server side

Time:06-12

I have tried several times but failed, I have also dissected the [image] and pdf libraries, but I don't understand how to create a widget to an image on the server side only,

does anyone know how to make a widget to an image on serverside only?

here's the code I tried

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:ui' as ui; 
void main() async {
  GlobalKey globalKey = GlobalKey();
  RepaintBoundary(
    key: globalKey,
    child: Container(
      padding: const EdgeInsets.all(50),
      child: const Text(
        "Hello world",
      ),
    ),
  );
  RenderRepaintBoundary boundary = globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
  ui.Image image = await boundary.toImage();
  ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData!.buffer.asUint8List();
  print(pngBytes);
}
 

CodePudding user response:

Flutter isn't meant to be used on the server side, but if you simply want to render an image with some text on the server, there are dart libraries for that.

For example, using the Hello World image

  • Related