LCOV - code coverage report
Current view: top level - src/ui/client/helpers - anchored_helper_widget.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 20 74 27.0 %
Date: 2020-12-04 18:41:24 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/material.dart';
       2             : import 'package:pal/src/ui/shared/utilities/element_finder.dart';
       3             : 
       4             : 
       5             : class AnchoredHelper extends StatefulWidget {
       6             : 
       7             :   final BuildContext subPageContext;
       8             : 
       9             :   final String keySearch;
      10             : 
      11           0 :   AnchoredHelper(this.subPageContext, this.keySearch);
      12             : 
      13           0 :   @override
      14           0 :   _AnchoredHelperState createState() => _AnchoredHelperState();
      15             : }
      16             : 
      17             : class _AnchoredHelperState extends State<AnchoredHelper> {
      18             : 
      19             :   Offset currentPos;
      20             : 
      21             :   Size anchorSize;
      22             : 
      23             :   Rect writeArea;
      24             : 
      25           0 :   @override
      26             :   void didChangeDependencies() {
      27           0 :     super.didChangeDependencies();
      28           0 :     WidgetsBinding.instance.addPostFrameCallback((_) {
      29           0 :       ElementFinder elementFinder = ElementFinder(widget.subPageContext);
      30           0 :       var element = elementFinder.searchChildElement(widget.keySearch);
      31           0 :       setState(() {
      32           0 :         anchorSize = element.bounds.size;
      33           0 :         currentPos = element.offset;
      34           0 :         writeArea = elementFinder.getLargestAvailableSpace(element);
      35             :       });
      36             :     });
      37             :   }
      38             : 
      39           0 :   @override
      40             :   Widget build(BuildContext context) {
      41           0 :     return Material(
      42             :       color: Colors.transparent,
      43           0 :       child: Stack(
      44           0 :         children: [
      45           0 :           Positioned.fill(
      46           0 :             child: Visibility(
      47           0 :               visible: this.currentPos != null,
      48           0 :               child: SizedBox(
      49           0 :                 child: CustomPaint(
      50           0 :                   painter: AnchoredFullscreenPainter(
      51           0 :                     currentPos: this.currentPos,
      52           0 :                     anchorSize: this.anchorSize,
      53             :                     padding: 16
      54             :                   )
      55             :                 )
      56             :               ),
      57             :             )
      58             :           ),
      59           0 :           Positioned.fromRect(
      60           0 :             rect: writeArea ?? Rect.largest,
      61           0 :             child: Column(
      62             :               mainAxisAlignment: MainAxisAlignment.center,
      63             :               crossAxisAlignment: CrossAxisAlignment.center,
      64           0 :               children: [
      65           0 :                 Padding(
      66             :                   padding: const EdgeInsets.all(8.0),
      67           0 :                   child: Text(
      68             :                     "This is a text i wanna show. My user needs to understand this part of the screen",
      69             :                     textAlign: TextAlign.center,
      70           0 :                     style: TextStyle(
      71             :                       fontSize: 21
      72             :                     ),
      73             :                   ),
      74             :                 ),
      75           0 :                 _buildPositivFeedback(),
      76           0 :                 _buildNegativFeedback(),
      77             :               ],
      78             :             ),
      79             :           )
      80             :         ],
      81             :       ),
      82             :     );
      83             :   }
      84             : 
      85           0 :   Widget _buildNegativFeedback() {
      86           0 :     return Padding(
      87             :         padding: const EdgeInsets.only(top: 16.0),
      88           0 :         child: InkWell(
      89           0 :           key: ValueKey("negativeFeedback"),
      90           0 :           child: Text(
      91             :             "This is not helping",
      92           0 :             style: TextStyle(
      93             :               // color: widget.textColor, fontSize: 10
      94             :             ),
      95             :             textAlign: TextAlign.center,
      96             :           ),
      97             :           // onTap: this.widget.onTrigger,
      98             :         ),
      99             :     );
     100             :   }
     101             : 
     102           0 :   Widget _buildPositivFeedback() {
     103           0 :     return Padding(
     104             :         padding: const EdgeInsets.only(top: 24.0),
     105           0 :         child: InkWell(
     106           0 :           key: ValueKey("positiveFeedback"),
     107           0 :           child: Text(
     108             :             "Ok, thanks !",
     109           0 :             style: TextStyle(
     110             :               // color: widget.textColor,
     111             :               fontSize: 18,
     112             :               decoration: TextDecoration.underline,
     113             :             ),
     114             :             textAlign: TextAlign.center,
     115             :           ),
     116             :           // onTap: this.widget.onTrigger,
     117             :         ),
     118             :     );
     119             :   }
     120             : }
     121             : 
     122             : 
     123             : class AnchoredFullscreenPainter extends CustomPainter {
     124             : 
     125             :   final Offset currentPos;
     126             : 
     127             :   final double padding;
     128             : 
     129             :   final Size anchorSize;
     130             : 
     131             :   final double area = 24.0 * 24.0;
     132             : 
     133           1 :   AnchoredFullscreenPainter({this.currentPos, this.anchorSize, this.padding = 0});
     134             : 
     135           1 :   @override
     136             :   void paint(Canvas canvas, Size size) {
     137           1 :     Paint clearPainter = Paint()
     138           1 :       ..blendMode = BlendMode.clear
     139           1 :       ..isAntiAlias = true;
     140           1 :     Paint bgPainter = Paint()
     141           2 :       ..color = Colors.lightGreenAccent.withOpacity(.6)
     142           1 :       ..style = PaintingStyle.fill
     143           1 :       ..isAntiAlias = true;
     144           3 :     canvas.saveLayer(Offset.zero & size, Paint());
     145           4 :     canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), bgPainter);
     146             :     // canvas.drawCircle(currentPos, radius, clearPainter);
     147             :     // canvas.drawRect(currentPos & anchorSize, clearPainter);
     148           2 :     if(padding > 0) {
     149           0 :       canvas.drawRect(Rect.fromLTWH(
     150           0 :         currentPos.dx - padding /2,
     151           0 :         currentPos.dy - padding /2,
     152           0 :         anchorSize.width + padding,
     153           0 :         anchorSize.height + padding), clearPainter);
     154             :     } else {
     155           2 :       canvas.drawRect(Rect.fromLTWH(
     156           2 :         currentPos.dx,
     157           2 :         currentPos.dy,
     158           2 :         anchorSize.width,
     159           2 :         anchorSize.height), clearPainter);
     160             :     }
     161             : 
     162           1 :     canvas.restore();
     163             :   }
     164             : 
     165           1 :   @override
     166             :   bool shouldRepaint(AnchoredFullscreenPainter oldDelegate) {
     167           3 :     return oldDelegate.currentPos != currentPos;
     168             :   }
     169             : 
     170           0 :   @override
     171             :   bool hitTest(Offset position) {
     172           0 :     if(currentPos == null)
     173             :       return false;
     174           0 :     var distance = (position - currentPos).distanceSquared;
     175           0 :     if(distance <= area) {
     176             :       return true;
     177             :     }
     178             :     return false;
     179             :   }
     180             : }

Generated by: LCOV version 1.14