import 'package:flutter/material.dart';

enum SlideDirection { up, down }

class SlideHide extends StatefulWidget {
  final Widget child;
  final bool hide; // 是否隐藏
  final SlideDirection direction;
  final Duration duration;

  const SlideHide({
    super.key,
    required this.child,
    this.hide = false,
    this.direction = SlideDirection.up,
    this.duration = const Duration(milliseconds: 200),
  });

  @override
  State<SlideHide> createState() => _SlideHideState();
}

class _SlideHideState extends State<SlideHide> with SingleTickerProviderStateMixin {
  late final AnimationController _controller;
  late final Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: widget.duration,
    );
    _animation = Tween<Offset>(
      begin: Offset.zero,
      end: widget.direction == SlideDirection.up ? const Offset(0, -1) : const Offset(0, 1),
    ).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut));

    if (widget.hide) {
      _controller.value = 1;
    }
  }

  @override
  void didUpdateWidget(covariant SlideHide oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.hide != widget.hide) {
      if (widget.hide) {
        _controller.forward();
      } else {
        _controller.reverse();
      }
    }
  }


  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: _animation,
      child: widget.child,
    );
  }
}
