omchiii / model_viewer_plus.dart

A Flutter widget for rendering interactive 3D models in the glTF and GLB formats.
https://pub.dev/packages/model_viewer_plus
Apache License 2.0
83 stars 48 forks source link

state doesnt update when parameters are changed #99

Open Ahadu-ray opened 6 months ago

Ahadu-ray commented 6 months ago

The state of the model_viewer doesn't change when a parameter is changed. For instance, the model being displayed is not updated when the src parameter's value is changed via setState or with state management tooks

corvusalbus commented 6 months ago

Same in my case. change src or variantName does nothing.

corvusalbus commented 6 months ago

I found out some workaround. It's not ideal but for some application will be ok. setState not refresh ModelViewer. To force it you can add key to ModelViewer and change this key when you want change some variable. it's work with everything but it not change it smootly. so for example when you change cameraTarget then your model disappear and appear with new cameraTarget.


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

  @override
  State<SamplePage > createState() => _SamplePageState();
}

class _SamplePageState extends State<SamplePage>{
  String _src = 'oldModelSrc';
  Key _refreshKey = UniqueKey();

  @override
  Widget build(BuildContext context){
    return Scaffold(
      body: Column(
        children:[
          ElevatedButton(
            onPressed: () => setState(() {
              _src = 'newModelSrc';
              _refreshKey = UniqueKey();
            }),
            child: const Text('New Model'),
          ),
          ModelViewer(
            key: _refreshKey,
            src: _src,
          )
        ]
      )
    )
  }
}