Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  void onUiKitViewCreated(int id) {
    // Execute your function here
    print('UiKitView is ready with ID: $id');
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      // After the app is resumed, use MethodChannel to get the UiKitView controller and call the function.
      final MethodChannel methodChannel = MethodChannel('myMethodChannel');
      methodChannel.invokeMethod<void>('getUiKitViewController').then((result) {
        onUiKitViewCreated(result);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return UiKitView(
      viewType: 'myUiKitViewType',
      onPlatformViewCreated: (int id) {
        // Save the view id to call the function later.
        _controller = id;
      },
    );
  }
}