1

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

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 Painter 实现了 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,所以我不确定编译器为什么会抛出这个异常。

任何帮助/建议将不胜感激。

4

1 回答 1

2

在 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());
    }
  }
}
于 2018-05-28T16:07:46.193 回答