import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_ali_camera/camera_export.dart'; typedef OnCameraViewCreated(CameraViewController controller); class CameraView extends StatefulWidget { final OnCameraViewCreated onCameraViewCreated; final Rect widgetRect; // use for measure view size on iOS const CameraView({Key key, this.onCameraViewCreated, this.widgetRect}) : super(key: key); @override _CameraViewState createState() => _CameraViewState(); } class _CameraViewState extends State { static const String viewType = "com.i2edu.cameraLib"; @override Widget build(BuildContext context) { return Platform.isAndroid ? AndroidView( viewType: viewType, creationParams: {}, creationParamsCodec: const StandardMessageCodec(), onPlatformViewCreated: onPlatformViewCreated, ) : UiKitView( viewType: viewType, creationParams: { "x": widget.widgetRect.left, "y": widget.widgetRect.top, "width": widget.widgetRect.width, "height": widget.widgetRect.height, }, creationParamsCodec: const StandardMessageCodec(), onPlatformViewCreated: onPlatformViewCreated, ); } void onPlatformViewCreated(int viewId) { final CameraViewController controller = CameraViewController(id: viewId); if (widget.onCameraViewCreated != null) { widget.onCameraViewCreated(controller); } } }