Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- // TODO: Horizontal
- enum PositionedSliderDirection { VERTICAL_UPPER, VERTICAL_LOWER }
- class PositionedSlider extends StatefulWidget {
- final PositionedSliderDirection direction;
- final EdgeInsetsGeometry padding;
- final EdgeInsetsGeometry margin;
- final double left;
- final double right;
- final double top;
- final double bottom;
- final double width;
- final double heightFrom;
- final double heightTo;
- final Color color;
- final Widget child;
- final Duration duration;
- final Function onReverted;
- final Function onForwarded;
- final Function onAnimating;
- const PositionedSlider({
- Key key,
- this.direction = PositionedSliderDirection.VERTICAL_UPPER,
- this.padding,
- this.margin,
- this.left,
- this.right,
- this.top,
- this.bottom,
- this.width,
- @required this.heightFrom,
- @required this.heightTo,
- this.color,
- @required this.child,
- this.duration,
- this.onReverted,
- this.onForwarded,
- this.onAnimating,
- }) : super(key: key);
- @override
- PositionedSliderState createState() => PositionedSliderState();
- }
- class PositionedSliderState extends State<PositionedSlider>
- with SingleTickerProviderStateMixin {
- AnimationController _controller;
- Animation<double> _animation;
- void forceAnimation() {
- if (_controller.isAnimating) return;
- if (_controller.isDismissed) _controller.forward();
- if (_controller.isCompleted) _controller.reverse();
- }
- @override
- void initState() {
- super.initState();
- _controller = AnimationController(
- duration: widget.duration ?? Duration(milliseconds: 500),
- vsync: this,
- )..addListener(() {
- if (_controller.isCompleted && widget.onForwarded != null)
- widget.onForwarded();
- if (_controller.isDismissed && widget.onReverted != null)
- widget.onReverted();
- if (_controller.isAnimating && widget.onAnimating != null)
- widget.onAnimating(_controller.value);
- });
- _animation = Tween<double>(begin: widget.heightFrom, end: widget.heightTo)
- .animate(_controller);
- }
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- onPanUpdate: (details) {
- if (details.delta.dy > 0) {
- if (_controller.isAnimating) return;
- if (_controller.isDismissed) {
- _controller.forward();
- return;
- }
- } else {
- if (_controller.isAnimating) return;
- if (_controller.isCompleted) {
- _controller.reverse();
- return;
- }
- }
- },
- child: Stack(
- children: <Widget>[
- Positioned(
- left: widget.left,
- top: widget.top,
- right: widget.right,
- bottom: widget.bottom,
- child: AnimatedBuilder(
- animation: _animation,
- builder: (BuildContext context, Widget child) {
- return Container(
- padding: widget.padding,
- margin: widget.margin,
- width: widget.width,
- height: _animation.value,
- color: widget.color,
- child: widget.child,
- );
- },
- ),
- ),
- ],
- ),
- );
- }
- }
- import 'package:flutter/material.dart';
- import 'package:sca/app/shared/widgets/positioned_slider.dart';
- class Teste extends StatefulWidget {
- @override
- _TesteState createState() => _TesteState();
- }
- class _TesteState extends State<Teste> {
- var sliderKey = GlobalKey<PositionedSliderState>();
- @override
- Widget build(BuildContext context) {
- Size size = MediaQuery.of(context).size;
- return PositionedSlider(
- key: sliderKey,
- direction: PositionedSliderDirection.VERTICAL_LOWER,
- duration: Duration(milliseconds: 300),
- width: size.width,
- heightFrom: 200,
- heightTo: size.height,
- color: Theme.of(context).primaryColor,
- onAnimating: (double completed) {
- print("estado da animação: ${completed.toStringAsFixed(2)}");
- },
- child: Column(
- children: <Widget>[
- Text('Conteúdo do Container animado'),
- RaisedButton(
- onPressed: (){
- sliderKey.currentState.forceAnimation();
- },
- child: Text('Alternar Animação do Slider'),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment