LCOV - code coverage report
Current view: top level - src/providers - zoom_provider.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 32 32 100.0 %
Date: 2021-10-03 15:54:23 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             :   final Widget child;
       6             : 
       7           1 :   const ZoomBuilder({
       8             :     Key? key,
       9             :     required this.child,
      10           1 :   }) : super(key: key);
      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             :   final ZoomState state;
      35             :   final ValueChanged<ZoomState> onStateChanged;
      36             :   final double levelChange = 0.25;
      37             : 
      38           1 :   const ZoomProvider({
      39             :     required this.state,
      40             :     required this.onStateChanged,
      41             :     required Widget child,
      42             :     Key? key,
      43           1 :   }) : super(
      44             :           child: child,
      45             :           key: key,
      46             :         );
      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