Flutter BottomNavigationBar带有页面切换示例

直接上代码

class BottomNavigationBarPage extends StatefulWidget {
  const BottomNavigationBarPage({super.key});

  @override
  State<BottomNavigationBarPage> createState() => HomeState();
}

class HomeState extends State<BottomNavigationBarPage> {
  int _selectedIndex = 0;
  late final PageController _controller;
  List<Widget> list = [
    const MessagePage(),
    const MenuPage(),
    const SettingPage(),
  ];

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: _selectedIndex);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
        controller: _controller,
        physics: const NeverScrollableScrollPhysics(), //禁止滑动
        itemCount: list.length,
        itemBuilder: (context, index) => list[index],
      ),
      bot

猜你喜欢

转载自blog.csdn.net/xiaopihair123/article/details/125525342