make FilamentWidget const and display error on createViewer throwing exception

This commit is contained in:
Nick Fisher
2023-10-13 12:54:58 +08:00
parent 504cc8ca30
commit 64309eeb54

View File

@@ -55,18 +55,12 @@ class FilamentWidget extends StatefulWidget {
/// The content to render before the texture widget is available. /// The content to render before the texture widget is available.
/// The default is a solid red Container, intentionally chosen to make it clear that there will be at least one frame where the Texture widget is not being rendered. /// The default is a solid red Container, intentionally chosen to make it clear that there will be at least one frame where the Texture widget is not being rendered.
/// ///
late final Widget initial; final Widget? initial;
final void Function()? onResize; final void Function()? onResize;
FilamentWidget( const FilamentWidget(
{Key? key, required this.controller, this.onResize, Widget? initial}) {Key? key, required this.controller, this.onResize, this.initial})
: super(key: key) { : super(key: key);
if (initial != null) {
this.initial = initial;
} else {
this.initial = Container(color: Colors.red);
}
}
@override @override
_FilamentWidgetState createState() => _FilamentWidgetState(); _FilamentWidgetState createState() => _FilamentWidgetState();
@@ -81,6 +75,8 @@ class _FilamentWidgetState extends State<FilamentWidget> {
bool _resizing = false; bool _resizing = false;
String? _error;
Timer? _resizeTimer; Timer? _resizeTimer;
void _handleStateChange(AppLifecycleState state) async { void _handleStateChange(AppLifecycleState state) async {
@@ -135,7 +131,13 @@ class _FilamentWidgetState extends State<FilamentWidget> {
} }
var size = ((context.findRenderObject()) as RenderBox).size; var size = ((context.findRenderObject()) as RenderBox).size;
widget.controller.createViewer(size.width.toInt(), size.height.toInt()); try {
widget.controller.createViewer(size.width.toInt(), size.height.toInt());
} catch (err) {
setState(() {
_error = err.toString();
});
}
}); });
_textureIdListener = widget.controller.textureId.listen((int? textureId) { _textureIdListener = widget.controller.textureId.listen((int? textureId) {
@@ -160,9 +162,17 @@ class _FilamentWidgetState extends State<FilamentWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_error != null) {
return Container(
color: Colors.white,
child: Column(children: [
const Text("A fatal error was encountered"),
Text(_error!)
]));
}
return LayoutBuilder(builder: ((context, constraints) { return LayoutBuilder(builder: ((context, constraints) {
if (_textureId == null) { if (_textureId == null) {
return widget.initial; return widget.initial ?? Container(color: Colors.red);
} }
var texture = Texture( var texture = Texture(
key: ObjectKey("texture_$_textureId"), key: ObjectKey("texture_$_textureId"),