camera_view.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_ali_camera/camera_export.dart';
  6. typedef OnCameraViewCreated(CameraViewController controller);
  7. class CameraView extends StatefulWidget {
  8. final OnCameraViewCreated onCameraViewCreated;
  9. final Rect widgetRect; // use for measure view size on iOS
  10. const CameraView({Key key, this.onCameraViewCreated, this.widgetRect}) : super(key: key);
  11. @override
  12. _CameraViewState createState() => _CameraViewState();
  13. }
  14. class _CameraViewState extends State<CameraView> {
  15. static const String viewType = "com.i2edu.cameraLib";
  16. @override
  17. Widget build(BuildContext context) {
  18. return Platform.isAndroid
  19. ? AndroidView(
  20. viewType: viewType,
  21. creationParams: {},
  22. creationParamsCodec: const StandardMessageCodec(),
  23. onPlatformViewCreated: onPlatformViewCreated,
  24. )
  25. : UiKitView(
  26. viewType: viewType,
  27. creationParams: {
  28. "x": widget.widgetRect.left,
  29. "y": widget.widgetRect.top,
  30. "width": widget.widgetRect.width,
  31. "height": widget.widgetRect.height,
  32. },
  33. creationParamsCodec: const StandardMessageCodec(),
  34. onPlatformViewCreated: onPlatformViewCreated,
  35. );
  36. }
  37. void onPlatformViewCreated(int viewId) {
  38. final CameraViewController controller = CameraViewController(id: viewId);
  39. if (widget.onCameraViewCreated != null) {
  40. widget.onCameraViewCreated(controller);
  41. }
  42. }
  43. }