LCOV - code coverage report
Current view: top level - src/providers - zoom_provider.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 33 33 100.0 %
Date: 2021-10-28 11:30:47 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/cupertino.dart';
       2             : import 'package:widgetbook/src/providers/zoom_state.dart';
       3             : 
       4             : class ZoomBuilder extends StatefulWidget {
       5           1 :   const ZoomBuilder({
       6             :     Key? key,
       7             :     required this.child,
       8           1 :   }) : super(key: key);
       9             : 
      10             :   final Widget child;
      11             : 
      12           1 :   @override
      13           1 :   _ZoomBuilderState createState() => _ZoomBuilderState();
      14             : }
      15             : 
      16             : class _ZoomBuilderState extends State<ZoomBuilder> {
      17             :   ZoomState state = ZoomState.normal();
      18             : 
      19           1 :   @override
      20             :   Widget build(BuildContext context) {
      21           1 :     return ZoomProvider(
      22           1 :       state: state,
      23           1 :       onStateChanged: (ZoomState state) {
      24           2 :         setState(() {
      25           1 :           this.state = state;
      26             :         });
      27             :       },
      28           2 :       child: widget.child,
      29             :     );
      30             :   }
      31             : }
      32             : 
      33             : class ZoomProvider extends InheritedWidget {
      34           1 :   const ZoomProvider({
      35             :     required this.state,
      36             :     required this.onStateChanged,
      37             :     required Widget child,
      38             :     Key? key,
      39           1 :   }) : super(
      40             :           child: child,
      41             :           key: key,
      42             :         );
      43             : 
      44             :   final ZoomState state;
      45             :   final ValueChanged<ZoomState> onStateChanged;
      46           1 :   double get levelChange => 0.25;
      47             : 
      48           1 :   static ZoomProvider? of(BuildContext context) {
      49           1 :     return context.dependOnInheritedWidgetOfExactType<ZoomProvider>();
      50             :   }
      51             : 
      52           1 :   void zoomIn() {
      53           1 :     onStateChanged(
      54           1 :       ZoomState(
      55           4 :         zoomLevel: state.zoomLevel + levelChange,
      56             :       ),
      57             :     );
      58             :   }
      59             : 
      60           1 :   void setScale(double scale) {
      61           1 :     onStateChanged(
      62           1 :       ZoomState(
      63             :         zoomLevel: scale,
      64             :       ),
      65             :     );
      66             :   }
      67             : 
      68           1 :   void zoomOut() {
      69           1 :     onStateChanged(
      70           1 :       ZoomState(
      71           5 :         zoomLevel: (state.zoomLevel - levelChange).clamp(
      72             :           0.25,
      73             :           999,
      74             :         ),
      75             :       ),
      76             :     );
      77             :   }
      78             : 
      79           1 :   void resetToNormal() {
      80           1 :     onStateChanged(
      81           1 :       ZoomState.normal(),
      82             :     );
      83             :   }
      84             : 
      85           1 :   @override
      86             :   bool updateShouldNotify(covariant ZoomProvider oldWidget) {
      87           3 :     return oldWidget.state != state ||
      88           3 :         oldWidget.onStateChanged != onStateChanged;
      89             :   }
      90             : }

Generated by: LCOV version 1.15