Flutter 场景切换动画实现

前言

在应用中,我们经常会遇到切换组件的场景,比如点击一个按钮后,将当前的图片为另一张图片;或者是翻转卡片,显示卡片详情。在 Flutter 中提供了 AnimatedSwitcher 这个动画组件来实现页面内的场景切换。
AnimatedSwitcher.gif

AnimatedSwitcher 介绍

AnimatedSwitcher 通过动效完成其子组件的切换,默认的效果是 FadeTransition。当其子组件发生改变的时候,就会按设定的转变效果进行转换。AnimatedSwitcher的构造方法如下:

const AnimatedSwitcher({
  Key? key,
  this.child,
  required this.duration,
  this.reverseDuration,
  this.switchInCurve = Curves.linear,
  this.switchOutCurve = Curves.linear,
  this.transitionBuilder = AnimatedSwitcher.defaultTransitionBuilder,
  this.layoutBuilder = AnimatedSwitcher.defaultLayoutBuilder,
})

与之前其他的动画组件不同,AnimatedSwitcher的参数除了 d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flutter 中,你可以使用 `CupertinoTabBar` 和 `CupertinoTabScaffold` 来实现带有动画的 TabBar 切换效果。下面是一个简单的示例代码: ```dart import 'package:flutter/cupertino.dart'; class MyTabScreen extends StatefulWidget { @override _MyTabScreenState createState() => _MyTabScreenState(); } class _MyTabScreenState extends State<MyTabScreen> { int _currentIndex = 0; @override Widget build(BuildContext context) { return CupertinoTabScaffold( tabBar: CupertinoTabBar( items: [ BottomNavigationBarItem( icon: Icon(CupertinoIcons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.search), label: 'Search', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.person), label: 'Profile', ), ], currentIndex: _currentIndex, onTap: (index) { setState(() { _currentIndex = index; }); }, ), tabBuilder: (BuildContext context, int index) { return CupertinoTabView( builder: (BuildContext context) { switch (index) { case 0: return HomeScreen(); case 1: return SearchScreen(); case 2: return ProfileScreen(); default: return Container(); } }, ); }, ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Home'), ), child: Center( child: Text('Home Screen'), ), ); } } class SearchScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Search'), ), child: Center( child: Text('Search Screen'), ), ); } } class ProfileScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Profile'), ), child: Center( child: Text('Profile Screen'), ), ); } } ``` 在这个示例中,我们使用 `CupertinoTabScaffold` 和 `CupertinoTabBar` 来创建底部的 TabBar,然后在 `tabBuilder` 方法中根据索引值切换显示不同的页面。在 `onTap` 回调中,我们可以更新 `_currentIndex` 的值来实现切换 Tab 时的动画效果。 以上是一个基本的示例,你可以根据自己的需求进行定制和修改。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岛上码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值