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

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;
});
},
),
),
],
),
);
}
}发布于 2022-05-12 18:16:05
在本例中,我们可以使用AnimatedSize小部件。为了在Stack中对齐小部件,我们需要定位小部件,如Positioned、Align.
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,
),
),
],
),
],
),
),
),
),发布于 2022-05-12 16:29:18
从列的两个子列中删除展开。
https://stackoverflow.com/questions/72217930
复制相似问题