порхание, виджет на изображение без добавления виджета на экран

#flutter

Вопрос:

Мне нужно конвертировать данные изображения из виджета без визуализации

вот пример ссылки.
этот модл не поддерживает «нулевую безопасность»
, поэтому я пытаюсь сделать его модулем самостоятельно.
я хочу сделать это как пример шаблона кода
, но я новичок
, я не знаю api рендеринга flutter

мне нужна помощь

Вот код, который я пытаюсь ввести.

 import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class WidgetToImage {
  static Future<dynamic> toImage(Widget widget, Size size, double devicePixelRatio) async {
    final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();
    final PipelineOwner pipelineOwner = PipelineOwner();
    final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
    final MeasurementView rootView = pipelineOwner.rootNode = MeasurementView();

    final RenderObjectToWidgetElement<RenderBox> element = RenderObjectToWidgetAdapter<RenderBox>(
      container: rootView,
      child: widget,
    ).attachToRenderTree(buildOwner);

    buildOwner.buildScope(element);
        buildOwner.finalizeTree();

    rootView.scheduleInitialLayout();
    pipelineOwner.flushLayout();
    pipelineOwner.flushCompositingBits();
    pipelineOwner.flushPaint();
    
    // ui.Image image = await repaintBoundary.toImage();
    // ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    // print('byteData $image');
    // return byteData;
    
  }
}

class MeasurementView extends RenderBox
    with RenderObjectWithChildMixin<RenderBox> {
  @override
  void performLayout() {
    print(child);
    child?.layout(const BoxConstraints(), parentUsesSize: true);
    size = child!.size;
  }

  @override
  void debugAssertDoesMeetConstraints() => true;
}

 

Комментарии:

1. Какие сообщения об ошибках вы получаете?

Ответ №1:

вы можете использовать davinci, это нулевая безопасность:

 import 'package:davinci/davinci.dart';
import 'package:davinci/core/davinci_capture.dart';
import 'package:flutter/material.dart';

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  ///1.create a globalkey variable
  GlobalKey imageKey;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ///2. wrap the desired widget with Davinci widget
            Davinci(
              builder: (key) {
                ///3. set the widget key to the globalkey
                imageKey = key;
                return Container(
                  height: 150,
                  width: double.infinity,
                  color: Colors.black,
                  child: Padding(
                    padding: const EdgeInsets.all(18.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          height: 50,
                          width: 50,
                          color: Colors.red,
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
            Padding(
              padding: const EdgeInsets.all(38.0),
              child: TextButton(
                onPressed: () async {
                  ///4. pass the globalKey varible to DavinciCapture.click.
                  await DavinciCapture.click(imageKey);
                },
                child: Text('capture',
                    style: TextStyle(
                      color: Colors.white,
                    )),
              ),
            ),
            TextButton(
              onPressed: () async {
                ///If the widget was not in the widget tree
                ///pass the widget that has to be converted into image.
                await DavinciCapture.offStage(PreviewWidget());
              },
              child: Text('Capture'),
            )
          ],
        ),
      ),
    );
  }
}

/// This widget is not mounted when the App is mounted.
class PreviewWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}