You need to enable JavaScript to run this app. Flutter使用curved_navigation_bar配置页面导航及设置默认首页
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用curved_navigation_bar配置页面导航及设置默认首页

curved_navigation_bar 导航逻辑配置步骤

首先你需要先确保已经创建好news.dart、cells.dart、home.dart、book.dart、info.dart五个页面,且每个页面都导出了对应的Widget类,比如NewsPage、CellsPage这类。

    1. 替换原有导入,引入五个页面对应的文件
// 删掉原来的 import 'package:statistik_malang/body.dart'; 替换为下面的导入,路径换成你自己的文件实际路径
import 'news.dart';
import 'cells.dart';
import 'home.dart';
import 'book.dart';
import 'info.dart';
    1. 在状态类中添加当前选中索引的变量,默认值设为2
class _HomeScreenState extends State<HomeScreen> {
  // 添加这一行,默认选中索引2,对应home页面
  int _currentIndex = 2;
  // 定义页面对象列表,顺序和底部导航索引一一对应
  final List<Widget> _pages = const [
    NewsPage(),
    CellsPage(),
    HomePage(),
    BookPage(),
    InfoPage(),
  ];
  @override
  Widget build(BuildContext context) {
    // ...原有代码
  }
}
    1. 修改Scaffold的body属性,绑定当前页面对应的Widget
      把原来的body: Body(),改成body: _pages[_currentIndex],
    1. 给CurvedNavigationBar添加index属性,完善onTap回调
      在CurvedNavigationBar的配置里添加index: _currentIndex,,然后修改onTap方法:
onTap: (index) {
  setState(() {
    _currentIndex = index;
  });
},

完整修改后代码

import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:statistik_malang/constants.dart';
// 替换为你自己的页面实际路径
import 'news.dart';
import 'cells.dart';
import 'home.dart';
import 'book.dart';
import 'info.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  // 默认选中索引2,对应home页面
  int _currentIndex = 2;
  // 页面列表,顺序和底部导航索引一一对应
  final List<Widget> _pages = const [
    NewsPage(),
    CellsPage(),
    HomePage(),
    BookPage(),
    InfoPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      extendBody: true,
      bottomNavigationBar: CurvedNavigationBar(
        // 绑定当前选中索引
        index: _currentIndex,
        backgroundColor: Colors.transparent,
        buttonBackgroundColor: kPrimaryColor,
        animationDuration: const Duration(milliseconds: 300),
        height: 50,
        items: <Widget>[
          SvgPicture.asset(
            "assets/icons/news.svg",
            width: 20.0,
            height: 20.0,
            color: _currentIndex == 0 ? Colors.white : Colors.grey,
          ),
          SvgPicture.asset(
            "assets/icons/cells.svg",
            width: 20.0,
            height: 20.0,
            color: _currentIndex == 1 ? Colors.white : Colors.grey,
          ),
          SvgPicture.asset(
            "assets/icons/home.svg",
            width: 20.0,
            height: 20.0,
            color: _currentIndex == 2 ? Colors.white : Colors.grey,
          ),
          SvgPicture.asset(
            "assets/icons/book.svg",
            width: 20.0,
            height: 20.0,
            color: _currentIndex == 3 ? Colors.white : Colors.grey,
          ),
          SvgPicture.asset(
            "assets/icons/information.svg",
            width: 20.0,
            height: 20.0,
            color: _currentIndex == 4 ? Colors.white : Colors.grey,
          ),
        ],
        onTap: (index) {
          // 点击底部导航时更新当前索引,刷新页面
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

注意:如果你的页面类名不是NewsPage、CellsPage这类,需要替换为你自己实际的类名,导入路径也要对应你项目的文件结构。

内容的提问来源于stack exchange,提问作者lukman azhari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 12:36:03