1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| class MainPage extends StatefulWidget { const MainPage({Key? key}) : super(key: key);
@override State<MainPage> createState() => _MainPageState(); }
class _MainPageState extends State<MainPage> { late PageController pageController; var pages = [ KeepAliveWrapper(child: WeiChatPage(),), DevicePage(), MinePage() ]; var titles = ["首页","设备","我的"]; var currentIndex = 0; @override void initState() { super.initState(); pageController = new PageController( initialPage: 0, keepPage: true, );
} @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leadingWidth: 70, leading: currentIndex == 1 ? TextButton( style: ButtonStyle( padding: MaterialStateProperty.all(EdgeInsets.only(left: 10)), ), onPressed: (){}, child: Text("切换设备",style: TextStyle( color: Colors.black, fontSize: 14 ),), ) : null, actions: currentIndex == 1 ? [ IconButton(onPressed: (){}, icon: Icon(Icons.add,color: Colors.black,)) ] : null, backgroundColor: Colors.white, elevation: 0.2, centerTitle: true, title: Text(titles[currentIndex],style: TextStyle( color: Colors.black ),), ), body: PageView( controller: pageController, physics: const NeverScrollableScrollPhysics(), onPageChanged: (index) { setState(() { currentIndex = index; }); }, children: pages, ), bottomNavigationBar: BottomNavigationBar( currentIndex: currentIndex, type: BottomNavigationBarType.fixed, onTap: (index) { pageController.jumpToPage(index); setState(() { currentIndex = index; }); }, items: [ BottomNavigationBarItem( icon: Icon(Icons.messenger_outlined), label:titles[0]), BottomNavigationBarItem( icon: Icon(Icons.settings), label: titles[1]), BottomNavigationBarItem( icon: Icon(Icons.person), label: titles[2]), ], ), ); } }
|