123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import 'package:fis_measure/interfaces/date_types/int_size.dart';
- import 'package:fis_measure/interfaces/process/workspace/application.dart';
- import 'package:fis_measure/view/paint/ai_patint_controller.dart';
- import 'package:fis_measure/view/paint/parts/dash_line.dart';
- import 'package:fis_measure/view/paint/parts/dots.dart';
- import 'package:fis_measure/view/paint/parts/rectangle.dart';
- import 'package:fis_measure/view/player/controller.dart';
- import 'package:fis_measure/view/player/enums.dart';
- import 'package:fis_measure/view/player/events.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- class AIPaintInfo extends StatefulWidget {
- /// 播放器控制器
- final VidPlayerController controller;
- final double? width;
- final double? height;
- const AIPaintInfo(
- this.controller, {
- Key? key,
- this.width,
- this.height,
- }) : super(key: key);
- @override
- State<AIPaintInfo> createState() => _AIPaintInfoState();
- }
- class _AIPaintInfoState extends State<AIPaintInfo> {
- late final aiPatintController = Get.put(
- AiPatintController(
- widget.controller,
- ),
- );
- @override
- void initState() {
- //监听Widget是否绘制完毕
- super.initState();
- widget.controller.eventHandler.addListener(onControllerEvent);
- }
- @override
- void dispose() {
- super.dispose();
- // widget.controller.eventHandler.removeListener(onControllerEvent);
- // _removeListenrs();
- }
- void onControllerEvent(Object sender, VidPlayerEvent e) {
- if (e is VidPlayerStatusChangeEvent) {
- if (!widget.controller.hasView) {
- setState(() {});
- }
- }
- if (e is VidPlayerFrameIndexChangeEvent) {
- onFrameChanged(e);
- }
- if (e is VidPlayerBrightnessChangeEvent) {
- updateFrame();
- }
- if (e is VidPlayerContrastChangeEvent) {
- updateFrame();
- }
- }
- void onFrameChanged(VidPlayerFrameIndexChangeEvent e) {
- setState(() {
- aiPatintController.state.frameIndex = e.index;
- });
- }
- void updateFrame() {
- setState(() {});
- }
- @override
- void didUpdateWidget(covariant AIPaintInfo oldWidget) {
- if (oldWidget.controller != widget.controller) {
- throw UnsupportedError("[VidTestPlayer] unsupport replace controller.");
- }
- super.didUpdateWidget(oldWidget);
- }
- @override
- Widget build(BuildContext context) {
- Widget? child;
- switch (widget.controller.status) {
- case VidPlayStatus.init:
- child = Container(child: const Text("Loading"));
- break;
- case VidPlayStatus.ready:
- child = Container(child: const Text("Ready"));
- break;
- case VidPlayStatus.loadFail:
- child = Container(child: const Text("Load fail"));
- break;
- case VidPlayStatus.play:
- case VidPlayStatus.pause:
- child = LayoutBuilder(builder: (context, constraints) {
- final canvasWidth = constraints.maxWidth;
- late final frameDataWidth =
- aiPatintController.application.frameData?.width;
- late final widthScale = (canvasWidth / frameDataWidth!);
- if (!aiPatintController.onGetAIResultsInfo(widthScale)) {
- return const SizedBox();
- } else {
- return CustomMultiChildLayout(
- delegate: _LayerLayoutDelegate(),
- children: [
- if (aiPatintController.aiDetectedObject.isNotEmpty) ...[
- if (!aiPatintController.isPlay) ...[
- LayoutId(
- id: _LayerLayoutIds.dots,
- child: CustomPaint(
- painter: PaintAIDots(aiPatintController.aiResultsList),
- ),
- ),
- LayoutId(
- id: _LayerLayoutIds.dashLine,
- child: CustomPaint(
- painter: PaintAIDashLine(
- aiPatintController.p1,
- aiPatintController.p2,
- aiPatintController.p3,
- aiPatintController.p4,
- ),
- ),
- ),
- ],
- ],
- if (aiPatintController.isPlay)
- LayoutId(
- id: _LayerLayoutIds.rectangle,
- child: CustomPaint(
- painter: PaintAIRectangle(
- aiPatintController.left,
- aiPatintController.top,
- aiPatintController.width,
- aiPatintController.height,
- ),
- ),
- ),
- ],
- );
- }
- });
- break;
- case VidPlayStatus.stop:
- case VidPlayStatus.dispose:
- child = Container(child: const Text("Closed"));
- break;
- }
- return buildBox(context, child);
- }
- Widget buildBox(BuildContext context, Widget child) {
- return Container(
- alignment: Alignment.center,
- child: child,
- );
- }
- }
- class _LayerLayoutDelegate extends MultiChildLayoutDelegate {
- Offset? layoutOffset;
- Size? layoutSize;
- _LayerLayoutDelegate();
- @override
- void performLayout(Size size) {
- final application = Get.find<IApplication>();
- final vidFrame = application.frameData;
- final imageSize = IntSize.fill(vidFrame?.width ?? 0, vidFrame?.height ?? 0);
- /// 以Contain方式填充布局,计算定位偏移量
- calcSize(size, imageSize);
- final offset = layoutOffset!;
- final renderSize = layoutSize!;
- /// 同步图像显示尺寸
- application.displaySize = renderSize;
- /// 其他层按播放器尺寸位置层叠布局
- layoutLayer(_LayerLayoutIds.dots, offset, renderSize);
- layoutLayer(_LayerLayoutIds.dashLine, offset, renderSize);
- layoutLayer(_LayerLayoutIds.rectangle, offset, renderSize);
- layoutLayer(_LayerLayoutIds.aiResult, offset, renderSize);
- }
- void layoutLayer(_LayerLayoutIds layoutId, Offset offset, Size size) {
- if (hasChild(layoutId)) {
- layoutChild(
- layoutId,
- BoxConstraints.loose(size),
- );
- positionChild(layoutId, offset);
- }
- }
- void calcSize(Size size, IntSize imageSize) {
- final parentWHRatio = size.width / size.height;
- final imageWHRatio = imageSize.width / imageSize.height;
- if (imageWHRatio < parentWHRatio) {
- // 高度撑满
- final layoutWidth = size.height * imageWHRatio;
- final layoutHeight = size.height;
- final offsetX = (size.width - layoutWidth) / 2;
- layoutOffset = Offset(offsetX, 0);
- layoutSize = Size(layoutWidth, layoutHeight);
- } else if (imageWHRatio > parentWHRatio) {
- // 宽度撑满
- final layoutWidth = size.width;
- final layoutHeight = size.width / imageWHRatio;
- final offsetY = (size.height - layoutHeight) / 2;
- layoutOffset = Offset(0, offsetY);
- layoutSize = Size(layoutWidth, layoutHeight);
- } else {
- layoutOffset = Offset.zero;
- layoutSize = size;
- }
- }
- @override
- bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
- return false;
- }
- }
- enum _LayerLayoutIds {
- /// 点
- dots,
- /// 虚线
- dashLine,
- /// 矩形
- rectangle,
- /// ai结果
- aiResult
- }
|