如何在flutter应用程序的所有屏幕上添加水印

s3fp2yjn  于 2022-11-25  发布在  Flutter
关注(0)|答案(3)|浏览(318)

大多数搜索结果显示添加水印的图像。。我需要的是添加文本水印到整个应用程序,覆盖所有屏幕或页面的应用程序。。如(测试版)或(学生版),以避免截图的应用程序尚未准备好的版本。

r7s23pms

r7s23pms1#

您可以使用这个类别:

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;
  }
}

并被用作小部件:

OverlayEntry _overlayEntry;

void addWatermark(BuildContext context, String watermark,
    {int rowCount = 3, int columnCount = 10}) async {
  if (_overlayEntry != null) {
    _overlayEntry.remove();
  }
  OverlayState overlayState = Overlay.of(context);
  _overlayEntry = OverlayEntry(
      builder: (context) => Watarmark(
            rowCount: rowCount,
            columnCount: columnCount,
            text: watermark,
          ));
  overlayState.insert(_overlayEntry);
  // return await _methodChannel.invokeMethod<void>("addWatermark", ['I am a watermark']);
}
zrfyljdw

zrfyljdw2#

下面是一个示例,说明如何使用Overlay在整个flutter应用程序上添加内容

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _navigatorKey = GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: WillPopScope(
        onWillPop: () async => !await _navigatorKey.currentState.maybePop(),
        child: LayoutBuilder(
          builder: (context, constraints) {
            WidgetsBinding.instance.addPostFrameCallback((_) => _insertOverlay(context));
            return Navigator(
              key: _navigatorKey,
              onGenerateRoute: (RouteSettings settings) {
                switch (settings.name) {
                  case '/page2':
                    return MaterialPageRoute(builder: (_) => Page2());
                  default:
                    return MaterialPageRoute(builder: (_) => Page1(_navigatorKey));
                }
              },
            );
          },
        ),
      ),
    );
  }

  void _insertOverlay(BuildContext context) {
    return Overlay.of(context).insert(
      OverlayEntry(builder: (context) {
        final size = MediaQuery.of(context).size;
        return Positioned(
          width: 130,
          height: 50,
          top: size.height - 200,
          left: size.width - 200,
          child: Material(
            color: Colors.transparent,
            child: GestureDetector(
              onTap: () => print('ON TAP OVERLAY!'),
              child: Center (child: Container(
                decoration: BoxDecoration(color: Colors.redAccent),
                child: Text('BETA VERSION')
              ),)
            ),
          ),
        );
      }),
    );
  }
}

class Page1 extends StatelessWidget {
  final GlobalKey<NavigatorState> navigatorKey;

  Page1(this.navigatorKey);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.green[200],
      appBar: AppBar(title: Text('Page1')),
      body: Container(
        alignment: Alignment.center,
        child: TextButton(
          child: Text('go to Page2'),
          onPressed: () => navigatorKey.currentState.pushNamed('/page2'),
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.yellow[200],
      appBar: AppBar(title: Text('back to Page1')),
      body: Container(
        alignment: Alignment.center,
        child: Text('Page 2'),
      ),
    );
  }
}
7xllpg7q

7xllpg7q3#

class WatermarkLogo extends StatelessWidget {
  const WatermarkLogo({super.key, required this.child});
  final Widget child;
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        Positioned.fill(child: child),
        IgnorePointer(
            child: Center(
                child: Opacity(
          opacity: 0.025,
          child: Padding(
              padding: const EdgeInsets.symmetric(
                horizontal: 50,
              ),
              child: Image.asset('assets/logo.png')),
        )))
      ],
    );
  }
}

相关问题