flutterdartstatesetstategesturedetector

Flutter / Why my model is not updating when i click on my image?


I working with the model-viewer library in Flutter and i'm making a list of 3D model so i want to change model when i click on a image i try to make a gesturedetector with a setstate when i click it it will change to another model but i don't know why it doesn't work Can i get some help please ?

Code:

main() async {
  runApp(MyWidget());
}


class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}





class _MyWidgetState extends State<MyWidget> {


  String _modelUrl = "assets/models/model1.glb";

  void changeModelUrl() {
    print("func change");
    setState(() {
      _modelUrl = "assets/models/model2.glb";
    });
  }


@override
Widget build(BuildContext context) {
    
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          title: Text('3DGallery'),

        ),
        body: Row(
          children: [
            // ListView de trois boutons
            Container(
              width: 120,
              child: ListView(
                children: [
                  Container(
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.black)
                    ),
                    child: GestureDetector(
                      onTap: () {
                        print("tapmasque");
                        changeModelUrl();
                      },
                      child: Image.asset(
                        'assets/poster/masquesenegal.webp',
                        width: 150,
                      ),
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.black)
                    ),
                    child: GestureDetector(
                      onTap: () {
                        print("tapradio");
                        changeModelUrl();
                        print(_modelUrl);

                      },
                      child: Image.asset(
                        'assets/poster/radio.webp',
                        width: 150,
                      ),
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.black)
                    ),
                    child: GestureDetector(
                      onTap: () {
                        print("tapdodo");
                        changeModelUrl();
                      },
                      child: Image.asset(
                        'assets/poster/dodo.webp',
                        width: 150,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: ModelViewer(
                id: "oeuvres3D",
                //src: 'assets/models/poste_recepteur.glb',
                src: _modelUrl,
                skyboxImage: "https://i.ibb.co/2gy0xVv/Shutterstock-1157860666.jpg",
                //skyboxImage: "https://modelviewer.dev/shared-assets/environments/spruit_sunrise_1k_HDR.hdr",
                exposure: 3,
                ar: true,
                arModes: ['scene-viewer', 'webxr', 'quick-look'],
                autoRotate: true,
                cameraControls: true,
                disableZoom: true,
                relatedJs: js,
                innerModelViewerHtml: _html2,
                overwriteNodeValidatorBuilder: myNodeValidatorBuilder,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

I tried to put the setstate directly into the gesturedetector , same problem


Solution

  • That's because the package is not doing anything in the didUpdateWidget method : https://github.com/drydart/model_viewer.dart/blob/master/lib/src/model_viewer.dart#L114

    What you can do is use a Key to force to recreate the Widget, like this:

                 ModelViewer(
                    key: ValueKey(_modelUrl),
                    id: "oeuvres3D",
                    src: _modelUrl,