flutter 屏幕截图,超出屏幕截图
用于flutter 自定义截图,超出屏幕的部分也可以截取到
import'dart:io';import'dart:typed_data';import'dart:ui';import'package:flutter/material.dart';import'package:flutter/rendering.dart';classScreenShotPageextendsStatefulWidget{
@override
_ScreenShotPageState createState()=>_ScreenShotPageState();}class_ScreenShotPageStateextendsState<ScreenShotPage>{
GlobalKey _rootWidgetKey =GlobalKey();
@override
Widget build(BuildContext context){returnMaterial(
child:Scaffold(
appBar:AppBar(title:Text('屏幕截图')),
body:SafeArea(
child:Stack(
children:<Widget>[Positioned(
left:0,
right:0,
top:0,
bottom:60,
child:SingleChildScrollView(
scrollDirection: Axis.vertical,
child:RepaintBoundary(
key: _rootWidgetKey,
child:Container(
width: double.infinity,
color:Color(0xffe8eaed),
child:Column(children:<Widget>[Column(children:_listWidget())]),),)),),Positioned(left:0, right:0, bottom:0, height:60, child:_bottomWidget()),],),),),);}
List<Widget>_listWidget(){
List<Widget> _list =List();for(int index =0; index <20; index++){
_list.add(Container(height:30, child:Center(child:Text('屏幕截图$index'))));}return _list;}
Widget _bottomWidget(){returnContainer(
padding: EdgeInsets.fromLTRB(16,0,16,0),
height:60,
color: Colors.black.withOpacity(0.8),
child:Center(
child:RaisedButton(
color: Colors.blue,
shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
onPressed:(){_screenshotImg();},
child:Text('完成', style:TextStyle(fontSize:16, color: Colors.white)),),),);}
Future _screenshotImg()async{try{
RenderRepaintBoundary boundary = _rootWidgetKey.currentContext.findRenderObject();var _dpr = window.devicePixelRatio;var image =await boundary.toImage(pixelRatio: _dpr);_getImgFilePath(image,'root/filePath').then((filePath){print('filePath:$filePath');});}catch(e){print(e);}}static Future<String>_getImgFilePath(var image, String filePath)async{
ByteData byteData =await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
File file =File('$filePath/237128773298739.png');if(!file.existsSync()){
file.createSync();}
file.writeAsBytesSync(pngBytes);return file.path;}}
发布评论