首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列子AnimatedContainer

列子AnimatedContainer
EN

Stack Overflow用户
提问于 2022-05-12 15:01:33
回答 2查看 393关注 0票数 0

我想动画一个AnimatedContainer的子高度,其中子部件是一个列小部件。虽然我可以动画该列,但是该列的子列是单独的,而不是单个Widget导致列的子列重叠的:

代码语言:javascript
复制
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;
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}
EN

回答 2

Stack Overflow用户

发布于 2022-05-12 18:16:05

在本例中,我们可以使用AnimatedSize小部件。为了在Stack中对齐小部件,我们需要定位小部件,如PositionedAlign.

代码语言:javascript
复制
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,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
  ),
),
票数 1
EN

Stack Overflow用户

发布于 2022-05-12 16:29:18

从列的两个子列中删除展开。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72217930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档