fix aspect

This commit is contained in:
Nick Fisher
2023-08-08 13:05:08 +08:00
parent 4ec9dbe756
commit 3e982e2993
4 changed files with 81 additions and 54 deletions

View File

@@ -70,7 +70,6 @@ class FilamentController {
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
_textureId =
await _channel.invokeMethod("createTexture", [size.width, size.height]);
_textureIdController.add(_textureId);
await _channel
.invokeMethod("createFilamentViewer", [size.width, size.height]);
@@ -93,6 +92,7 @@ class FilamentController {
_initialized.complete(true);
_assetManager = await _channel.invokeMethod("getAssetManager");
_textureIdController.add(_textureId);
}
Future resize(int width, int height,

View File

@@ -1,6 +1,7 @@
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'dart:async';
@@ -55,78 +56,98 @@ class FilamentWidget extends StatefulWidget {
}
class _FilamentWidgetState extends State<FilamentWidget> {
StreamSubscription? _listener;
StreamSubscription? _initializationListener;
StreamSubscription? _textureIdListener;
int? _textureId;
bool _resizing = false;
bool _hasViewer = false;
@override
void initState() {
_listener = widget.controller.onInitializationRequested.listen((_) {
_initializationListener =
widget.controller.onInitializationRequested.listen((_) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
var size = ((context.findRenderObject()) as RenderBox).size;
print(
"Requesting creation of Filament back-end texture/viewer for viewport size $size");
await widget.controller
.createViewer(size.width.toInt(), size.height.toInt());
print("Filament texture/viewer created.");
_listener!.cancel();
_listener = null;
setState(() {
_hasViewer = true;
});
_initializationListener!.cancel();
_initializationListener = null;
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
var size = ((context.findRenderObject()) as RenderBox).size;
widget.controller.resize(size.width.toInt(), size.height.toInt());
print("RESIZED IN POST FRAME CALLBACK TO $size");
});
setState(() {});
});
});
_textureIdListener = widget.controller.textureId.listen((int? textureId) {
setState(() {
_textureId = textureId;
});
setState(() {});
});
super.initState();
}
@override
void dispose() {
_listener?.cancel();
_initializationListener?.cancel();
_textureIdListener?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: widget.controller.textureId,
builder: (ctx, AsyncSnapshot<int?> textureId) {
if (textureId.data == null) {
return Container();
}
return LayoutBuilder(builder: ((context, constraints) {
print("constraints $constraints");
if (_textureId == null) {
return Container(color: Colors.transparent);
}
var texture = Texture(
key: ObjectKey("texture_${textureId.data}"),
textureId: textureId.data!,
filterQuality: FilterQuality.high,
);
return LayoutBuilder(
builder: ((context, constraints) => SizedBox(
height: constraints.maxHeight,
width: constraints.maxWidth,
child: ResizeObserver(
onResized: (Size oldSize, Size newSize) async {
WidgetsBinding.instance.addPostFrameCallback((_) async {
setState(() {
_resizing = true;
});
var texture = Texture(
key: ObjectKey("texture_$_textureId"),
textureId: _textureId!,
filterQuality: FilterQuality.high,
);
return SizedBox(
height: constraints.maxHeight,
width: constraints.maxWidth,
child: ResizeObserver(
onResized: (Size oldSize, Size newSize) async {
if (!_hasViewer) {
return;
}
print("RESIZE OBSERVER $newSize");
WidgetsBinding.instance.addPostFrameCallback((_) async {
setState(() {
_resizing = true;
});
await widget.controller.resize(
newSize.width.toInt(), newSize.height.toInt());
WidgetsBinding.instance
.addPostFrameCallback((_) async {
setState(() {
_resizing = false;
});
});
});
},
child: Platform.isLinux
? _resizing
? Container()
: Transform(
alignment: Alignment.center,
transform: Matrix4.rotationX(
pi), // TODO - this rotation is due to OpenGL texture coordinate working in a different space from Flutter, can we move this to the C++ side somewhere?
child: texture)
: texture))));
});
await widget.controller
.resize(newSize.width.toInt(), newSize.height.toInt());
WidgetsBinding.instance.addPostFrameCallback((_) async {
setState(() {
_resizing = false;
});
});
});
},
child: Platform.isLinux
? _resizing
? Container()
: Transform(
alignment: Alignment.center,
transform: Matrix4.rotationX(
pi), // TODO - this rotation is due to OpenGL texture coordinate working in a different space from Flutter, can we move this to the C++ side somewhere?
child: texture)
: texture));
}));
}
}