import 'package:flutter/cupertino.dart'; class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _currentIndex = 0; late PageController _pageController; @override void initState() { super.initState(); _pageController = PageController(initialPage: _currentIndex); } @override void dispose() { _pageController.dispose(); super.dispose(); } void _onTabTapped(int index) { setState(() { _currentIndex = index; }); _pageController.animateToPage( index, duration: const Duration(milliseconds: 300), curve: Curves.easeInOut, ); } void _onPageChanged(int index) { setState(() { _currentIndex = index; }); } @override Widget build(BuildContext context) { return CupertinoPageScaffold( child: Column( children: [ Expanded( child: PageView( controller: _pageController, onPageChanged: _onPageChanged, children: [ _buildFirstPage(), _buildSecondPage(), _buildThirdPage(), ], ), ), _buildBottomNavigationBar(), ], ), ); } Widget _buildFirstPage() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(CupertinoIcons.home, size: 64, color: CupertinoColors.activeBlue), const SizedBox(height: 16), const Text( '首页', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), const SizedBox(height: 8), const Text('这是第一个页面'), ], ), ); } Widget _buildSecondPage() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(CupertinoIcons.chat_bubble_2, size: 64, color: CupertinoColors.activeGreen), const SizedBox(height: 16), const Text( '消息', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), const SizedBox(height: 8), const Text('这是第二个页面'), ], ), ); } Widget _buildThirdPage() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(CupertinoIcons.person, size: 64, color: CupertinoColors.systemPurple), const SizedBox(height: 16), const Text( '我的', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), const SizedBox(height: 8), const Text('这是第三个页面'), ], ), ); } Widget _buildBottomNavigationBar() { return Container( decoration: BoxDecoration( border: Border( top: BorderSide(color: CupertinoColors.separator, width: 0.5), ), ), child: CupertinoTabBar( currentIndex: _currentIndex, onTap: _onTabTapped, items: const [ BottomNavigationBarItem( icon: Icon(CupertinoIcons.home), label: '首页', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.chat_bubble_2), label: '消息', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.person), label: '我的', ), ], ), ); } }