简体   繁体   English

带有 Column 子级的 AnimatedContainer

[英]AnimatedContainer with Column child

I would like to animate the child height of an AnimatedContainer where the child is a Column widget.我想为AnimatedContainer的孩子高度设置动画,其中孩子是 Column 小部件。 While I can animate the Column, the Column's children are animated individually rather than a single Widget causing the Column's children to overlap:虽然我可以为 Column 设置动画,但 Column 的子项是单独动画的,而不是单个 Widget 导致 Column 的子项重叠:

描述问题的图像。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(title: 'Animated Container Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool expanded = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Stack(
        children: [
          AnimatedContainer(
            clipBehavior: Clip.hardEdge,
            duration: const Duration(milliseconds: 1000),
            curve: Curves.easeInOut,
            color: Colors.black12,
            height: expanded ? 100 : 0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: const [
                    Expanded(
                      child: Text(
                        'Top',
                        style: TextStyle(
                          color: Colors.blue,
                          fontSize: 32,
                        ),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        'Bottom',
                        style: TextStyle(
                          color: Colors.blue,
                          fontSize: 32,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          Center(
            child: ElevatedButton(
              child: const Text(
                'Toggle Animate',
              ),
              onPressed: () {
                setState(() {
                  expanded = !expanded;
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}

We can useAnimatedSize widget in this case.在这种情况下,我们可以使用AnimatedSize小部件。 And to align widget inside Stack we need positioned widget like Positioned , Align ...为了在Stack中对齐小部件,我们需要定位小部件,例如PositionedAlign ...

Align(
  alignment: Alignment.topCenter,
  child: AnimatedSize(
    clipBehavior: Clip.hardEdge,
    duration: const Duration(milliseconds: 1000),
    curve: Curves.easeInOut,
    alignment: Alignment.topCenter,
    child: !expanded
        ? const SizedBox()
        : Container(
            height: 100,
            color: Colors.black12,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: const [
                    Text(
                      'Top',
                      style: TextStyle(
                        color: Colors.blue,
                        fontSize: 32,
                      ),
                    ),
                    Text(
                      'Bottom',
                      style: TextStyle(
                        color: Colors.blue,
                        fontSize: 32,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
  ),
),

从 Column 的两个子项中删除扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM