123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import 'package:flutter/material.dart';
- /// 可伸缩容器
- class ExpandableContainer extends StatefulWidget {
- final Widget child;
- final bool expanded;
- final double collapsedHeight;
- final double expandedHeight;
- final double fixedWidth;
- ExpandableContainer({
- required this.child,
- this.expanded = false,
- this.collapsedHeight = 40.0,
- this.expandedHeight = 200.0,
- this.fixedWidth = 250.0,
- });
- @override
- _ExpandableContainerState createState() => _ExpandableContainerState();
- }
- class _ExpandableContainerState extends State<ExpandableContainer>
- with SingleTickerProviderStateMixin {
- late AnimationController _controller;
- late Animation<double> _heightFactor;
- @override
- void initState() {
- super.initState();
- _controller = AnimationController(
- vsync: this,
- duration: Duration(milliseconds: 500),
- );
- final double initialHeightFactor = widget.expanded ? 1.0 : 0.0;
- _heightFactor = _controller
- .drive(
- CurveTween(curve: Curves.fastOutSlowIn),
- )
- .drive(
- Tween<double>(begin: initialHeightFactor, end: 1.0),
- );
- if (widget.expanded) {
- _controller.value = 1.0;
- }
- }
- @override
- void didUpdateWidget(ExpandableContainer oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (widget.expanded != oldWidget.expanded) {
- if (widget.expanded) {
- _controller.forward();
- } else {
- _controller.reverse();
- }
- }
- }
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
- void _toggleExpanded() {
- print("toggleExpanded");
- if (_controller.isAnimating) {
- return;
- }
- final bool isExpanded = _heightFactor.value == 1.0;
- _controller.fling(
- velocity: isExpanded ? -2.0 : 2.0,
- );
- }
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: _toggleExpanded,
- child: AnimatedSize(
- duration: Duration(milliseconds: 500),
- child: AnimatedBuilder(
- animation: _heightFactor,
- builder: (BuildContext context, Widget? child) {
- return Container(
- width: widget.fixedWidth,
- height: widget.collapsedHeight +
- (_heightFactor.value *
- (widget.expandedHeight - widget.collapsedHeight)),
- child: child,
- );
- },
- child: widget.child,
- ),
- ),
- );
- }
- }
|