render 0.1.4 copy "render: ^0.1.4" to clipboard
render: ^0.1.4 copied to clipboard

A package to render any native static and moving flutter widgets to exportable formats

example/lib/main.dart

import 'package:example/animated_example_widget.dart';
import 'package:example/animated_example_buttons.dart';
import 'package:flutter/material.dart';
import 'package:gallery_saver/gallery_saver.dart';
import 'package:render/render.dart';
import 'animated_example_controller.dart';
import 'animated_example_popup.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  late final Future<ExampleAnimationController> init;
  final RenderController renderController =
      RenderController(logLevel: LogLevel.debug);

  @override
  void initState() {
    init = ExampleAnimationController.create(this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    init.then((value) => print("done"));
    return Scaffold(
      appBar: AppBar(
        title: const Text("Render Example"),
      ),
      body: FutureBuilder(
        future: init,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          } else if (snapshot.connectionState == ConnectionState.done &&
              snapshot.hasData) {
            final functionController = snapshot.data!;
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  const Spacer(),
                  Render(
                    controller: renderController,
                    child: AnimatedExampleWidget(
                      exampleAnimationController: functionController,
                    ),
                  ),
                  const Spacer(),
                  NavigationButtons(
                    motionRenderCallback: () async {
                      functionController.play();
                      final stream = renderController.captureMotionWithStream(
                        functionController.duration,
                        settings: const MotionSettings(
                          pixelRatio: 5,
                          frameRate: 30,
                          simultaneousCaptureHandlers: 6,
                        ),
                        logInConsole: true,
                        format: Mp4Format(audio: [
                          RenderAudio.url(
                            Uri.parse(
                                "https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"),
                          ),
                          /*
                          RenderAudio.url(
                            Uri.parse(
                                "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"),
                            startTime: 40,
                            endTime: 45.4365,
                          ),
                           */
                        ]),
                      );
                      setState(() {
                        functionController.attach(stream);
                      });
                      final result = await stream.firstWhere(
                          (event) => event.isResult || event.isFatalError);
                      if (result.isFatalError) return;
                      displayResult(result as RenderResult);
                    },
                    exampleAnimationController: functionController,
                    imageRenderCallback: () async {
                      final imageResult = await renderController.captureImage(
                        format: ImageFormat.png,
                        settings: const ImageSettings(pixelRatio: 3),
                      );
                      displayResult(imageResult);
                    },
                  ),
                ],
              ),
            );
          } else {
            return Center(
              child: Text(
                "Error loading: ${snapshot.error}",
                style: const TextStyle(
                  color: Colors.red,
                ),
              ),
            );
          }
        },
      ),
    );
  }

  Future<void> displayResult(RenderResult result,
      [bool saveToGallery = false]) async {
    print("file exits: ${await result.output.exists()}");
    if (mounted) {
      showDialog(
        context: context,
        builder: (BuildContext context) => AnimatedExamplePopUp(
          context: context,
          result: result,
        ),
      );
    }
    if (saveToGallery) {
      GallerySaver.saveImage(result.output.path)
          .then((value) => print("saved export to gallery"));
    }
  }
}
85
likes
140
points
140
downloads

Publisher

unverified uploader

Weekly Downloads

A package to render any native static and moving flutter widgets to exportable formats

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

ffmpeg_kit_flutter_https_gpl, flutter, path_provider, provider, rich_console, uuid

More

Packages that depend on render