flutter 截图功能(截取控件)

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:ui' as ui;
import 'package:path_provider/path_provider.dart';

class ScrrenShop extends StatefulWidget {
  @override
  _ScrrenShopState createState() => _ScrrenShopState();
}

class _ScrrenShopState extends State<ScrrenShop> {
  // Timer? timer;
  String path = '';
  final GlobalKey globalKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    startTimer();
  }

  @override
  void dispose() {
    // timer?.cancel();
    super.dispose();
  }

  void startTimer() {
    // timer = Timer.periodic(Duration(seconds: 5), (Timer t) {
    //   captureScreenshot();
    // });
    Future.delayed(const Duration(seconds: 5), () {
      captureScreenshot();
    });
  }

  Future<void> captureScreenshot() async {
    try {
      if (globalKey.currentContext != null) {
        if (globalKey.currentContext!.findRenderObject() != null) {
          RenderObject boundary = globalKey.currentContext!.findRenderObject()!;
          RenderRepaintBoundary repaintBoundary =
              boundary as RenderRepaintBoundary;

          ui.Image image = await repaintBoundary.toImage();

          ByteData? byteData =
              await image.toByteData(format: ui.ImageByteFormat.png);
          if (byteData != null) {
            Uint8List? pngBytes = byteData.buffer.asUint8List();

            final directory = await getApplicationDocumentsDirectory();
            final imagePath =
                '${directory.path}/screenshot.png';
            File imageFile = File(imagePath);
            await imageFile.writeAsBytes(pngBytes);
            print(imageFile.path);
            mySetState(() {
              path = imageFile.path;
              // UserInfoUtil().setUserHeadPortrait(Global.userId, File(imageFile.path));
            });
            print('截屏的地址${path}');
            // 在这里可以用imageFile.path处理截图的路径,例如上传给服务器
          }
        }
      }
    } catch (e) {}
  }

  mySetState(callBack) {
    if (mounted) {
      setState(() {
        callBack();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Screenshot Example'),
        ),
        body: RepaintBoundary(
          key: globalKey,
          child: path == ''
              ? Container(
                  width: 400,
                  height: 400,
                  color: Colors.yellow,
                  child: Column(
                    children: [
                      Text('pasdfoi '),
                      Text('pasdfoi '),
                      Text('pasdfoi ')
                    ],
                  ),
                )
              : Center(
                  child: Image(
                    image: FileImage(File(path)),
                    width: 400,
                    height: 400,
                  ),
                ),
        ),
      ),
    );
  }
}

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值