0

I'm trying to get an effect like this.

I've got the logic working, but currently there seems to be no animation (even if I bump the ms up). What the best way to achieve this "collapsing" affect.

Here's my current code.

@override
  Widget build(BuildContext context) {
    comment = Provider.of<Comment>(context);
    return GestureDetector(
      onTap: () {
        comment.toggleVisibility();
      },
      child: AnimatedSwitcher(
        child: comment.isVisible
            ? _buildComment(context)
            : _buildCollapsedComment(context),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return ScaleTransition(
            scale: animation,
            child: child,
          );
        },
        duration: Duration(
          milliseconds: 3000,
        ),
      ),
    );
Peter R
  • 3,185
  • 23
  • 43
  • @someuser That doesn't do what I need. That has a static part of the widget. Not what I'm looking for. – Peter R Jan 05 '21 at 09:44

1 Answers1

1

Maybe This Helps you, I have used AnimatedSize on a Container to animate it's height with some Duration.

import 'package:flutter/material.dart';

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget>
    with TickerProviderStateMixin {
  bool listTileTapped = false;
  double size = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            //A simple widget Where we can tap to Expand its lower widget with some height and animation
            GestureDetector(
              onTap: () {
                setState(() {
                  listTileTapped = !listTileTapped;
                });
                if (listTileTapped) {
                  size = 300;
                } else {
                  size = 0;
                }
              },
              child: new Container(
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.black),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: new Text("Hello Click Me to Expand"),
                ),
              ),
            ),

            //This widgets is used for setting the size to the container
            AnimatedSize(
              curve: Curves
                  .fastOutSlowIn, //you can choose your suitable curver for the purpose
              vsync: this,
              duration: Duration(seconds: 3),
              child: new Container(
                height: size,
                color: Colors.red,
                child: Center(child: new Text("This is the child Here!")),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

EDITED:

I found this working solution too, just use this ExpandedSection class from this link

Mukul
  • 1,020
  • 6
  • 12
  • Is it possible to use this with dynamic sizes? I don't know the height of my widget until runtime. – Peter R Jan 05 '21 at 10:18
  • @PeterR Yes, but in that case you have to use Expanded Widget, Wait let me try this in my code, I will let you know the results. – Mukul Jan 05 '21 at 10:20
  • @PeterR Please check my Edited Answer, I have a solution link to your problem, Which works for dynamic Containers too. – Mukul Jan 05 '21 at 10:34