初学Flutter:实现底部导航切换

效果展示

flutter bottomNavBar

主要实现代码

入口文件:main.dart

import 'package:flutter/material.dart';
import 'package:flutter_demo/components/bottomNavBar.dart';
import 'package:flutter_demo/views/cart.dart';
import 'package:flutter_demo/views/cata.dart';
import 'package:flutter_demo/views/person.dart';
import 'package:flutter_demo/views/home.dart';

main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // title: 'UzumakiHan',
        theme: ThemeData(
          // Define the default brightness and colors.
          brightness: Brightness.light,
          primaryColor: Colors.lightBlue[800],
        ),
        home: const MyHome());
  }
}

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

  @override
  // ignore: library_private_types_in_public_api
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  List tabBodies = [
    const IndexHome(),
    const Cata(),
    const Cart(),
    const Person()
  ];
  int currentIndex = 0;
  List<BottomNavigationBarItem> bottomTabs = [
    const BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
    const BottomNavigationBarItem(icon: Icon(Icons.search), label: '分类'),
    const BottomNavigationBarItem(
        icon: Icon(Icons.shopping_cart), label: '购物车'),
    const BottomNavigationBarItem(
        icon: Icon(Icons.manage_accounts), label: '个人中心'),
  ];
  List<String> pageTitle = ['首页', '分类', '购物车', '个人中心'];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(pageTitle[currentIndex]),
          centerTitle: true,
        ),
        body: tabBodies[currentIndex],
        bottomNavigationBar: BottomNavBar(
          bottomTabs: bottomTabs,
          currentIndex: currentIndex,
          changeCurrentIndex: (index) {
            setState(() {
              currentIndex = index;
            });
          },
        ));
  }
}

底部导航组件 bottomNavBar.dart

// ignore_for_file: library_private_types_in_public_api

import 'package:flutter/material.dart';
class BottomNavBar extends StatefulWidget {
  const BottomNavBar(
      {super.key,
      required this.bottomTabs,
      required this.currentIndex,
      required this.changeCurrentIndex});
  final List<BottomNavigationBarItem> bottomTabs;
  final int currentIndex;
  final Function changeCurrentIndex;

  @override
  _BootomNavBarState createState() => _BootomNavBarState();
}

class _BootomNavBarState extends State<BottomNavBar> {
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: widget.currentIndex,
        items: widget.bottomTabs,
        onTap: (index) {
          widget.changeCurrentIndex(index);
        });
  }
}

下面是四个导航对应的页面

首页:home.dart

// ignore_for_file: library_private_types_in_public_api

import 'package:flutter/material.dart';

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

  @override
  _IndexHomeState createState() => _IndexHomeState();
}

class _IndexHomeState extends State<IndexHome> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return const Center(
        child: Text(
      '首页',
      style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
    ));
  }
}

分类:cata.dart

// ignore_for_file: library_private_types_in_public_api

import 'package:flutter/material.dart';

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

  @override
  _CataState createState() => _CataState();
}

class _CataState extends State<Cata> {
  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text("分类",
          style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold)),
    );
  }
}

购物车:cart.dart

// ignore_for_file: library_private_types_in_public_api

import 'package:flutter/material.dart';

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

  @override
  _CartState createState() => _CartState();
}

class _CartState extends State<Cart> {
  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text("购物车",
          style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold)),
    );
  }
}

个人中心:person.dart

// ignore_for_file: library_private_types_in_public_api

import 'package:flutter/material.dart';

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

  @override
  _IndexHomeState createState() => _IndexHomeState();
}

class _IndexHomeState extends State<IndexHome> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return const Center(
        child: Text(
      '首页',
      style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
    ));
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值