mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
use AnimatedSwitcher instead of AnimatedCrossFade
This commit is contained in:
@@ -20,13 +20,13 @@ class ExpandCard extends StatefulWidget {
|
||||
class _ExpandCardState extends State<ExpandCard>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const Duration duration = Duration(milliseconds: 300);
|
||||
bool expanded = false;
|
||||
bool selected = false;
|
||||
|
||||
double get size => expanded ? 200 : 100;
|
||||
double get size => selected ? 200 : 100;
|
||||
|
||||
void toggleExpanded() {
|
||||
setState(() {
|
||||
expanded = !expanded;
|
||||
selected = !selected;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -40,35 +40,30 @@ class _ExpandCardState extends State<ExpandCard>
|
||||
duration: duration,
|
||||
width: size,
|
||||
height: size,
|
||||
child: AnimatedCrossFade(
|
||||
child: AnimatedSwitcher(
|
||||
duration: duration,
|
||||
crossFadeState: expanded
|
||||
? CrossFadeState.showSecond
|
||||
: CrossFadeState.showFirst,
|
||||
// Use Positioned.fill() to pass the constraints to its children.
|
||||
// This allows the Images to use BoxFit.cover to cover the correct
|
||||
// size
|
||||
layoutBuilder: (Widget topChild, Key topChildKey,
|
||||
Widget bottomChild, Key bottomChildKey) {
|
||||
layoutBuilder:
|
||||
(Widget currentChild, List<Widget> previousChildren) {
|
||||
List<Widget> children = previousChildren;
|
||||
if (currentChild != null)
|
||||
children = children.toList()..add(currentChild);
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
Positioned.fill(
|
||||
key: bottomChildKey,
|
||||
child: bottomChild,
|
||||
),
|
||||
Positioned.fill(
|
||||
key: topChildKey,
|
||||
child: topChild,
|
||||
),
|
||||
children: [
|
||||
// Use Positioned.fill so that the child image receives the
|
||||
// constraints of the AnimatedContainer
|
||||
...children.map((c) => Positioned.fill(child: c)),
|
||||
],
|
||||
);
|
||||
},
|
||||
firstChild: Image.asset(
|
||||
child: selected
|
||||
? Image.asset(
|
||||
'assets/cat.jpg',
|
||||
key: ValueKey('cat'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
secondChild: Image.asset(
|
||||
)
|
||||
: Image.asset(
|
||||
'assets/wolf.jpg',
|
||||
key: ValueKey('wolf'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user