flutterdartflutter-layoutflutter-dependenciesflutter-image

How to add watermark over all screens in flutter app


Most search results showed adding watermarks to images .. what i need is to add text watermark to the whole app that covers all screens or pages of the app.. such as (beta version) or (student version) so as to avoid screenshots of not yet ready version of the app.


Solution

  • You can use this class:

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

    and use by this as a widget:

    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']);
    }