wemersonrv

PositionedSlider flutter

Dec 10th, 2019
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.41 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. // TODO: Horizontal
  4. enum PositionedSliderDirection { VERTICAL_UPPER, VERTICAL_LOWER }
  5.  
  6. class PositionedSlider extends StatefulWidget {
  7.   final PositionedSliderDirection direction;
  8.   final EdgeInsetsGeometry padding;
  9.   final EdgeInsetsGeometry margin;
  10.   final double left;
  11.   final double right;
  12.   final double top;
  13.   final double bottom;
  14.   final double width;
  15.   final double heightFrom;
  16.   final double heightTo;
  17.   final Color color;
  18.   final Widget child;
  19.   final Duration duration;
  20.   final Function onReverted;
  21.   final Function onForwarded;
  22.   final Function onAnimating;
  23.  
  24.   const PositionedSlider({
  25.     Key key,
  26.     this.direction = PositionedSliderDirection.VERTICAL_UPPER,
  27.     this.padding,
  28.     this.margin,
  29.     this.left,
  30.     this.right,
  31.     this.top,
  32.     this.bottom,
  33.     this.width,
  34.     @required this.heightFrom,
  35.     @required this.heightTo,
  36.     this.color,
  37.     @required this.child,
  38.     this.duration,
  39.     this.onReverted,
  40.     this.onForwarded,
  41.     this.onAnimating,
  42.   }) : super(key: key);
  43.  
  44.   @override
  45.   PositionedSliderState createState() => PositionedSliderState();
  46. }
  47.  
  48. class PositionedSliderState extends State<PositionedSlider>
  49.     with SingleTickerProviderStateMixin {
  50.   AnimationController _controller;
  51.   Animation<double> _animation;
  52.  
  53.   void forceAnimation() {
  54.     if (_controller.isAnimating) return;
  55.     if (_controller.isDismissed) _controller.forward();
  56.     if (_controller.isCompleted) _controller.reverse();
  57.   }
  58.  
  59.   @override
  60.   void initState() {
  61.     super.initState();
  62.  
  63.     _controller = AnimationController(
  64.       duration: widget.duration ?? Duration(milliseconds: 500),
  65.       vsync: this,
  66.     )..addListener(() {
  67.         if (_controller.isCompleted && widget.onForwarded != null)
  68.           widget.onForwarded();
  69.  
  70.         if (_controller.isDismissed && widget.onReverted != null)
  71.           widget.onReverted();
  72.  
  73.         if (_controller.isAnimating && widget.onAnimating != null)
  74.           widget.onAnimating(_controller.value);
  75.       });
  76.  
  77.     _animation = Tween<double>(begin: widget.heightFrom, end: widget.heightTo)
  78.         .animate(_controller);
  79.   }
  80.  
  81.   @override
  82.   Widget build(BuildContext context) {
  83.     return GestureDetector(
  84.       onPanUpdate: (details) {
  85.         if (details.delta.dy > 0) {
  86.           if (_controller.isAnimating) return;
  87.           if (_controller.isDismissed) {
  88.             _controller.forward();
  89.             return;
  90.           }
  91.         } else {
  92.           if (_controller.isAnimating) return;
  93.           if (_controller.isCompleted) {
  94.             _controller.reverse();
  95.             return;
  96.           }
  97.         }
  98.       },
  99.       child: Stack(
  100.         children: <Widget>[
  101.           Positioned(
  102.             left: widget.left,
  103.             top: widget.top,
  104.             right: widget.right,
  105.             bottom: widget.bottom,
  106.             child: AnimatedBuilder(
  107.               animation: _animation,
  108.               builder: (BuildContext context, Widget child) {
  109.                 return Container(
  110.                   padding: widget.padding,
  111.                   margin: widget.margin,
  112.                   width: widget.width,
  113.                   height: _animation.value,
  114.                   color: widget.color,
  115.                   child: widget.child,
  116.                 );
  117.               },
  118.             ),
  119.           ),
  120.         ],
  121.       ),
  122.     );
  123.   }
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. import 'package:flutter/material.dart';
  131. import 'package:sca/app/shared/widgets/positioned_slider.dart';
  132.  
  133. class Teste extends StatefulWidget {
  134.   @override
  135.   _TesteState createState() => _TesteState();
  136. }
  137.  
  138. class _TesteState extends State<Teste> {
  139.     var sliderKey = GlobalKey<PositionedSliderState>();
  140.  
  141.     @override
  142.     Widget build(BuildContext context) {
  143.         Size size = MediaQuery.of(context).size;
  144.  
  145.         return PositionedSlider(
  146.             key: sliderKey,
  147.             direction: PositionedSliderDirection.VERTICAL_LOWER,
  148.             duration: Duration(milliseconds: 300),
  149.             width: size.width,
  150.             heightFrom: 200,
  151.             heightTo: size.height,
  152.             color: Theme.of(context).primaryColor,
  153.             onAnimating: (double completed) {
  154.                 print("estado da animação: ${completed.toStringAsFixed(2)}");
  155.             },
  156.             child: Column(
  157.                 children: <Widget>[
  158.                     Text('Conteúdo do Container animado'),
  159.  
  160.                     RaisedButton(
  161.                         onPressed: (){
  162.                             sliderKey.currentState.forceAnimation();
  163.                         },
  164.                         child: Text('Alternar Animação do Slider'),
  165.                     ),
  166.                 ],
  167.             ),
  168.         );
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment