颤动:向画布添加图像,编译错误

时间:2018-05-28 14:29:42

标签: dart flutter

我正在尝试将字节缓冲区中的图像添加到canvas元素:

Uint8List image = bd.buffer.asUint8List(++offset, len);
await ui.decodeImageFromList(image, addIMage);

回调函数的位置,addImage()是

  void printImage(data){
    print("Image stuff");
    print(data);
    canvasPainter.addItem(data);
  }

Canvas画家实现了CustomPainter,如下所示:

class CanvasPainter implements CustomPainter{

 var items = new List();
 void addItems(var item){
 items.add(item);}
 @override
 void paint(Canvas canvas, Size size){
 for (var item in items){ 
   canvas.drawImage(item,new Offset(50.0,50.0),new Paint();
 }

 ...
}



class WriteScreen extends StatefulWidget {
  ScreenState state;

  @override
  ScreenState createState() => state = new ScreenState();
}


class ScreenState extends State<WriteScreen> {
  GestureDetector touch;
  CustomPaint canvas;
  CanvasPainter canvasPainter;

  @override
  void initState() {
    super.initState();
    canvasPainter = new CanvasPainter(const Color.fromRGBO(255, 255, 255, 1.0));
  }

  @override
  Widget build(BuildContext context) {
    touch = new GestureDetector(
      onPanStart: panStart
    );
    final mediaQueryData = MediaQuery.of(context);
    canvasPainter.size = mediaQueryData.size;
    canvas = new CustomPaint( painter: canvasPainter);
  }
}

我在CustomPainter类中收到编译时错误:

compiler message: lib/canvas_painter.dart:64:28: Error: A value of type '#lib1::Image' can't be assigned to a variable of type 'dart.ui::Image'.
compiler message: Try changing the type of the left hand side, or casting the right hand side to 'dart.ui::Image'.
compiler message:           canvas.drawImage(item, new Offset(50.0, 50.0), strokePaint);
compiler message:       

我对错误消息感到非常困惑,因为我从dart.ui导入Image,所以我不确定为什么编译器会抛出此异常。

非常感谢任何帮助/建议。

1 个答案:

答案 0 :(得分:1)

在CanvasPainter中添加一些类型,并使用任何新的错误消息更新问题。

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
....
class CanvasPainter extends CustomPainter {
  List<ui.Image> items = new List<ui.Image>();

  void addItems(ui.Image item) {
    items.add(item);
  }

  @override
  void paint(Canvas canvas, Size size) {
    for (ui.Image item in items) {
      canvas.drawImage(item, new Offset(50.0, 50.0), new Paint());
    }
  }
}