dart Flutter /为什么我的模型在我点击图像时没有更新?

fzsnzjdm  于 2023-04-27  发布在  Flutter
关注(0)|答案(1)|浏览(148)

我在Flutter中使用模型查看器库,我正在制作一个3D模型列表,所以我想在点击图像时更改模型,我尝试使用setstate制作一个gesturedetector,当我点击它时,它会更改为另一个模型,但我不知道为什么它不工作。
验证码:

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,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

我尝试将setstate直接放入gesturedetector,同样的问题

bogh5gae

bogh5gae1#

这是因为包在didUpdateWidget方法中没有做任何事情:https://github.com/drydart/model_viewer.dart/blob/master/lib/src/model_viewer.dart#L114
您可以使用Key强制重新创建Widget,如下所示:

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

相关问题