如何知道无状态小部件在视口中是否可见?

时间:2020-09-15 19:36:01

标签: flutter

在我的项目中,我有一个带有按钮的屏幕。当用户点击按钮时,我会调用一个库/依赖关系为我拍摄图像,然后当用户单击该屏幕的“完成”按钮时,它将返回该图像的路径。

我知道,如果我将无状态窗口小部件转换为有状态窗口小部件,则可以访问有状态类的mounted属性。但是我的问题是,对于无状态窗口小部件,是否有类似的东西,或者有什么办法可以使用户看到窗口小部件(我的场景)?

class LandingScreen extends StatelessWidget {
  void _onImageReceived() async {
    print('Running EdgeDetection...');
    try {
      final String path = await EdgeDetection.detectEdge;
      print('The saved image path is: $path');
    } on PlatformException {
      print('Failed to get cropped image path');
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return; // <= This is for Stateful widget that doesn't work in the Stateless widget
  }
}

0 个答案:

没有答案
相关问题