add resize timer to FilamentWidget
This commit is contained in:
@@ -67,10 +67,14 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
late final AppLifecycleListener _listener;
|
late final AppLifecycleListener _listener;
|
||||||
AppLifecycleState? _lastState;
|
AppLifecycleState? _lastState;
|
||||||
|
|
||||||
|
Timer? _resizeTimer;
|
||||||
|
|
||||||
void _handleStateChange(AppLifecycleState state) async {
|
void _handleStateChange(AppLifecycleState state) async {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case AppLifecycleState.detached:
|
case AppLifecycleState.detached:
|
||||||
print("Detached");
|
print("Detached");
|
||||||
|
_textureId = null;
|
||||||
|
|
||||||
await widget.controller.destroyViewer();
|
await widget.controller.destroyViewer();
|
||||||
await widget.controller.destroyTexture();
|
await widget.controller.destroyTexture();
|
||||||
break;
|
break;
|
||||||
@@ -95,6 +99,7 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
print("Size after resuming : $size");
|
print("Size after resuming : $size");
|
||||||
await widget.controller
|
await widget.controller
|
||||||
.createViewer(size.width.toInt(), size.height.toInt());
|
.createViewer(size.width.toInt(), size.height.toInt());
|
||||||
|
print("Created viewer Size after resuming");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -114,7 +119,8 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
|
|
||||||
_textureIdListener = widget.controller.textureId.listen((int? textureId) {
|
_textureIdListener = widget.controller.textureId.listen((int? textureId) {
|
||||||
var size = ((context.findRenderObject()) as RenderBox).size;
|
var size = ((context.findRenderObject()) as RenderBox).size;
|
||||||
print("Set texture ID to $textureId, current size is $size");
|
print(
|
||||||
|
"Received new texture ID $textureId at size $size (current textureID $_textureId)");
|
||||||
setState(() {
|
setState(() {
|
||||||
_textureId = textureId;
|
_textureId = textureId;
|
||||||
});
|
});
|
||||||
@@ -127,6 +133,7 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
_textureIdListener?.cancel();
|
_textureIdListener?.cancel();
|
||||||
_listener.dispose();
|
_listener.dispose();
|
||||||
|
_resizeTimer?.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +141,7 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return LayoutBuilder(builder: ((context, constraints) {
|
return LayoutBuilder(builder: ((context, constraints) {
|
||||||
if (_textureId == null) {
|
if (_textureId == null) {
|
||||||
return Container(color: Colors.red);
|
return Container(color: Colors.transparent);
|
||||||
}
|
}
|
||||||
|
|
||||||
var texture = Texture(
|
var texture = Texture(
|
||||||
@@ -148,10 +155,15 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
child: ResizeObserver(
|
child: ResizeObserver(
|
||||||
onResized: (Size oldSize, Size newSize) async {
|
onResized: (Size oldSize, Size newSize) async {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||||
|
if (!_resizing) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_resizing = true;
|
_resizing = true;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_resizeTimer?.cancel();
|
||||||
|
|
||||||
|
_resizeTimer = Timer(Duration(milliseconds: 500), () async {
|
||||||
await widget.controller
|
await widget.controller
|
||||||
.resize(newSize.width.toInt(), newSize.height.toInt());
|
.resize(newSize.width.toInt(), newSize.height.toInt());
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||||
@@ -161,6 +173,7 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
child: _resizing
|
child: _resizing
|
||||||
? Container()
|
? Container()
|
||||||
|
|||||||
Reference in New Issue
Block a user