我想在画布上绘制一个 200x200 像素的图像,然后对它做一些其他的事情。我拥有的图像是 900x690,所以理想情况下我想“居中裁剪”图像以使用 Android 术语。出于某种原因,该视频告诉我们使用 aFittedBox
然后 a SizedBox
。我也尝试将这两个都放入Container
. 我尝试将容器大小、大小框大小和客户画家Size
设置为 200x200,一切都会产生全屏图像。谁知道我做错了什么:)
这是代码:
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ui.Image image;
@override
void initState() {
super.initState();
load('images/noodlejpg.jpg').then((i) {
setState(() {
image = i;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(backgroundColor: Colors.blue, title: Text('I am a title')),
backgroundColor: Colors.teal,
body: SafeArea(
child: FittedBox(
child: SizedBox(
width: 200.0,
height: 200.0,
child: CustomPaint(
painter: MyPainter(image),
),
),
),
),
);
}
Future<ui.Image> load(String asset) async {
ByteData data = await rootBundle.load(asset);
ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
ui.FrameInfo fi = await codec.getNextFrame();
return fi.image;
}
}
class MyPainter extends CustomPainter {
ui.Image image;
MyPainter(this.image);
@override
void paint(Canvas canvas, Size size) {
var paint = new Paint();
paint.blendMode = BlendMode.srcIn;
paint.isAntiAlias = true;
if (image != null) canvas.drawImage(image, Offset.zero, paint);
}
@override
bool shouldRepaint(MyPainter oldDelegate) {
return image != oldDelegate.image;
}
}