flutter IOS切换tabbar 动画
时间: 2023-07-29 17:14:18 浏览: 312
在 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 时的动画效果。
以上是一个基本的示例,你可以根据自己的需求进行定制和修改。希望对你有帮助!
阅读全文