import 'package:flutter/material.dart'; import 'embedder_texture_platform_interface.dart'; enum OrientationEvent { undefined, portrait, landscape, portraitFlipped, landscapeFlipped, } class EmbedderTexture extends StatefulWidget { const EmbedderTexture({ super.key, required this.width, required this.height, }); final double width; final double height; @override State createState() => _EmbedderTextureState(); } class _EmbedderTextureState extends State { int _textureID = 0; int _cameraWidth = 0; int _cameraHeight = 0; OrientationEvent _orientation = OrientationEvent.undefined; @override initState() { super.initState(); EmbedderTexturePlatform.instance .create(widget.width, widget.height) .then((data) => setState(() { if (mounted) { _textureID = data['textureId']!; _cameraWidth = data['width']!; _cameraHeight = data['height']!; switch (data['orientation']!) { case 0: _orientation = OrientationEvent.portrait; break; case 90: _orientation = OrientationEvent.landscape; break; case 180: _orientation = OrientationEvent.portraitFlipped; break; case 270: _orientation = OrientationEvent.landscapeFlipped; break; default: _orientation = OrientationEvent.undefined; } debugPrint(data.toString()); } })); EmbedderTexturePlatform.instance.onChangeOrientation().listen((event) { setState(() { if (mounted) { _orientation = event; } }); }); } @override void dispose() { super.dispose(); EmbedderTexturePlatform.instance.remove(_textureID); } // @override // Widget build(BuildContext context) { // if (_textureID != 0) { // double w = 0; // double h = 0; // if (MediaQuery.of(context).orientation == Orientation.portrait) { // w = _cameraWidth * widget.height / (widget.width - 40); // h = widget.width - 40; // } else { // w = _cameraHeight.toDouble(); // h = widget.height - 40 - 56; // } // return Container( // color: Colors.black, // width: widget.width, // height: widget.height, // child: RotationTransition( // turns: AlwaysStoppedAnimation( // _orientation == OrientationEvent.portrait ? 90 / 360 : 0, // ), // child: SizedBox( // width: w, // height: h, // child: Texture(textureId: _textureID), // ), // ), // ); // } // return const SizedBox.shrink(); // } @override Widget build(BuildContext context) { if (_textureID != 0) { double w = 0; double h = 0; if (_orientation == OrientationEvent.portrait || _orientation == OrientationEvent.portraitFlipped) { w = _cameraWidth * widget.height / (widget.width - 40); h = widget.width - 40; } else { w = _cameraHeight.toDouble(); h = widget.height - 40 - 56; } AlwaysStoppedAnimation? turns; switch (_orientation) { case OrientationEvent.portrait: turns = const AlwaysStoppedAnimation(90 / 360); break; case OrientationEvent.portraitFlipped: turns = const AlwaysStoppedAnimation(270 / 360); break; case OrientationEvent.landscapeFlipped: turns = const AlwaysStoppedAnimation(180 / 360); break; default: turns = const AlwaysStoppedAnimation(0); } return Container( color: Colors.black, width: widget.width, height: widget.height, child: Center( child: RotationTransition( turns: turns, child: SizedBox( width: w, height: h, child: Texture(textureId: _textureID), ), ), ), ); } return const SizedBox.shrink(); } }