Is it possible to generate (and save as an image) interface or individual flutter widgets without displaying the program, in the background / console utility? For windows/Linux.
While this is a working variant, the program is still displayed:
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:io';
import 'package:intl/intl.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
const test = Text(
'Hello, World!',
style: TextStyle(fontSize: 24.0),
);
final boundaryKey = GlobalKey();
final RepaintBoundary repaintBoundary = RepaintBoundary(
key: boundaryKey,
child: test,
);
WidgetsBinding.instance.addPostFrameCallback((_) async {
DateTime now = DateTime.now();
var formatter = DateFormat('hh.mm.ss');
String formattedDate = formatter.format(now);
print(formattedDate);
final RenderRepaintBoundary boundary =
boundaryKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
final image = await boundary.toImage(pixelRatio: 3.0);
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final pngBytes = byteData!.buffer.asUint8List();
final file = File('results/$formattedDate.png');
await file.writeAsBytes(pngBytes);
print('Image saved to: ${file.path}');
});
runApp(MaterialApp(home: repaintBoundary));
}
New contributor
standart is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.