[flutter] More work on dress-up example.

This commit is contained in:
Mario Zechner 2022-11-17 18:42:09 +01:00
parent a3874256cd
commit 187808d543

View File

@ -1,3 +1,4 @@
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
@ -21,27 +22,30 @@ class DressUpState extends State<DressUp> {
super.initState();
SkeletonDrawable.fromAsset("assets/mix-and-match-pro.skel", "assets/mix-and-match.atlas").then((drawable) async {
for (var skin in drawable.skeletonData.getSkins()) {
//if (skin.getName() == "default") continue;
if (skin.getName() == "default") continue;
var skeleton = drawable.skeleton;
skeleton.setSkin(skin);
skeleton.setToSetupPose();
skeleton.updateWorldTransform();
var bounds = skeleton.getBounds();
var scale = 1 / (bounds.width > bounds.height ? bounds.width / thumbnailSize * 1.1 : bounds.height / thumbnailSize * 1.1);
var scale = 1 / (bounds.width > bounds.height ? bounds.width / thumbnailSize : bounds.height / thumbnailSize);
scale *= 0.9;
var recorder = ui.PictureRecorder();
var canvas = Canvas(recorder, const Rect.fromLTWH(0, 0, thumbnailSize, thumbnailSize));
var canvas = Canvas(recorder);
var bgColor = Random().nextInt(0xffffffff) | 0xff0000000;
var paint = Paint()
..color = const ui.Color(0xffff00ff)
..color = ui.Color(bgColor)
..style = PaintingStyle.fill;
canvas.drawRect(const Rect.fromLTWH(0, 0, thumbnailSize, thumbnailSize), paint);
canvas.scale(scale, scale);
canvas.translate(-bounds.x, -bounds.y);
canvas.drawRect(Rect.fromLTRB(-5, -5, 5, -5), paint..color = Colors.red);
drawable.renderToCanvas(canvas);
var imageData = await (await recorder.endRecording().toImage(thumbnailSize.toInt(), thumbnailSize.toInt())).toByteData(format: ui.ImageByteFormat.png);
_skinImages.add(Image.memory(imageData!.buffer.asUint8List(), fit: BoxFit.none));
_skinImages.add(Image.memory(imageData!.buffer.asUint8List(), fit: BoxFit.contain));
}
_drawable = drawable;
setState(() {});
@ -59,7 +63,7 @@ class DressUpState extends State<DressUp> {
Expanded(
child:ListView(
children: _skinImages.map((image) {
return SizedBox(width: 100, height: 100, child: image);
return SizedBox(width: 200, height: 200, child: image);
}).toList()
)
),