一、前言
现实生活中,我们想要对应用的开发程序内容进行管控,避免暴露出敏感信息。因此,在很多的pc端的开发页面上,经常能看见水印,这种水印的实现,在html、vue等技术上对于开发过的人来说十分的简单。当然,App端同样也有很多的敏感信息,避免暴露出去因此需要加上水印。
自己这次的开发需求上就遇见了,因为是第一次接触flutter,对与很多东西都不懂更别说全局设置什么水印了,所以,在网上查询了好久,诸如“flutter 添加水印”、“fluter设置水印”…查询出来的博客、页面,屁都不是,满屏幕的什么涂鸦的方法乱七八糟,所以自己从pub.dev上面找出来的demo,仔细调试了调试,贴出源码,为各位提供参考,实现效果如下:

实现代码如下:
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:disable_screenshots/disable_screenshots.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(home: RootApp());
}
}
class RootApp extends StatefulWidget {
@override
_RootAppState createState() => _RootAppState();
}
class _RootAppState extends State<RootApp> {
// 初始化插件
DisableScreenshots _plugin = DisableScreenshots();
// 监控截屏行为的stream
StreamSubscription<void> _screenshotsSubscription;
int _screenshotsCount = 0;
bool _disableScreenshots = false;
@override
void initState() {
super.initState();
_screenshotsSubscription = _plugin.onScreenShots.listen((event) {
setState(() {
_screenshotsCount++;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('冷月寒雪'),
),
body: Column(
children: <Widget>[
Center(
child: Text("监控到截屏次数:$_screenshotsCount"),
),
Center(
child: Text(_disableScreenshots ? "禁止截屏状态" : "允许截屏状态"),
),
RaisedButton(
onPressed: () {
// 添加默认样式的水印
_plugin.addWatermark(context, "默认水印",
rowCount: 4, columnCount: 8);
},
child: Text("添加默认水印")),
RaisedButton(
onPressed: () {
// 添加自定义widget当做水印
_plugin.addCustomWatermark(context,
Watarmark(rowCount: 3, columnCount: 10, text: "冷月韩雪"));
},
child: Text("添加自定义水印")),
RaisedButton(
onPressed: () {
// 移除水印
_plugin.removeWatermark();
},
child: Text("删除水印")),
RaisedButton(
onPressed: () async {
bool flag = !_disableScreenshots;
// 禁用或允许截屏(只支持iOS)
await _plugin.disableScreenshots(flag);
setState(() {
_disableScreenshots = flag;
});
},
child: Text(_disableScreenshots
? "允许截屏(仅android适用)"
: "禁用截屏(仅android适用)")),
RaisedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => Scaffold(
appBar: AppBar(
title: Text("我是新页面"),
),
body: Center(child: Text("new page")),
)));
},
child: Text("进入新页面"))
],
),
);
}
@override
void dispose() {
super.dispose();
//取消截屏监控可以调用cancel()方法
if (_screenshotsSubscription != null) {
_screenshotsSubscription.cancel();
}
}
}
class Watarmark extends StatelessWidget {
final int rowCount;
final int columnCount;
final String text;
const Watarmark(
{Key key,
@required this.rowCount,
@required this.columnCount,
@required this.text})
: super(key: key);
@override
Widget build(BuildContext context) {
return IgnorePointer(
child: Container(
child: Column(
children: creatColumnWidgets(),
)),
);
}
List<Widget> creatRowWdiges() {
List<Widget> list = [];
for (var i = 0; i < rowCount; i++) {
final widget = Expanded(
child: Center(
child: Transform.rotate(
angle: pi / 10,
child: Text(
text,
style: TextStyle(
color: Color(0x08000000),
fontSize: 18,
decoration: TextDecoration.none),
),
)));
list.add(widget);
}
return list;
}
List<Widget> creatColumnWidgets() {
List<Widget> list = [];
for (var i = 0; i < columnCount; i++) {
final widget = Expanded(
child: Row(
children: creatRowWdiges(),
));
list.add(widget);
}
return list;
}
}
需要依赖如下:

disable_screenshots:

本文介绍如何在Flutter应用中全局添加水印,包括默认水印和自定义水印的实现方法,同时提供了Android平台上的截屏禁用功能。通过详细代码示例,帮助开发者快速实现水印功能,保护应用内容不被随意截取。
最低0.47元/天 解锁文章
2477





