flutter tabbar切换选中,展示对应模块

import 'package:flutter/material.dart';



void main() {

  runApp(MyApp());

}



class MyApp extends StatelessWidget {

  const MyApp({super.key});



  @override

  Widget build(BuildContext context) {

    return const MaterialApp(title: 'contaniner', home: HomePage());

  }

}



class HomePage extends StatefulWidget {

  const HomePage({super.key});



  @override

  State<HomePage> createState() => _HomePageState();

}



class _HomePageState extends State<HomePage> {

  int currentIndex = 0;

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      floatingActionButton: Container(

        width: 60,

        height: 60,

        padding: const EdgeInsets.all(5),

        margin: EdgeInsets.only(top: 15),

        decoration: BoxDecoration(

          color: Colors.white,

          borderRadius: BorderRadius.circular(30),

        ),

        child: FloatingActionButton(

          backgroundColor: currentIndex == 2 ? Colors.red : Colors.blue,

          shape: RoundedRectangleBorder(

            borderRadius: BorderRadius.circular(40), //设置圆角

          ),

          onPressed: () => {setState(() => currentIndex = 2)},

          child: const Icon(Icons.add),

        ),

      ),

      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

      bottomNavigationBar: BottomNavigationBar(

        elevation: 80,

        fixedColor: Colors.red,

        backgroundColor: Colors.white,

        type: BottomNavigationBarType.fixed,

        iconSize: 33,

        currentIndex: currentIndex,

        onTap: (index) => setState(() => currentIndex = index),

        items: const [

          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),

          BottomNavigationBarItem(

              icon: Icon(Icons.card_giftcard), label: "购物车"),

          BottomNavigationBarItem(icon: Icon(Icons.settings), label: "设置"),

          BottomNavigationBarItem(icon: Icon(Icons.adb_outlined), label: "订单"),

          BottomNavigationBarItem(icon: Icon(Icons.people), label: "个人信息"),

        ],

      ),

      appBar: AppBar(

        title: const Center(child: Text('flutter bar')),

        // leading:  Icon(Icons.menu),

        leading: IconButton(

          onPressed: () => print("我是图标按钮1"),

          icon: Icon(Icons.ac_unit), //设置图标

          color: Colors.red, //设置按钮颜色

          splashColor: Colors.yellow, //设置水波纹

          highlightColor: Colors.purple, //设置高亮的颜色

          tooltip: '我是提示信息', //提示信息

        ),

        actions: const [

          Icon(Icons.settings),

          Icon(Icons.vaccines),

        ],

        backgroundColor: Colors.cyan[800],

        elevation: 0.0,

        centerTitle: true,

      ),

      //带索引的集合循环

      body:

          [HomeWidget(), CardWidget(), SettingWidget()].elementAt(currentIndex),

    );

  }

}



class HomeWidget extends StatelessWidget {

  const HomeWidget({super.key});



  @override

  Widget build(BuildContext context) {

    // return const Text('我是首页数据', style: TextStyle(color: Colors.red));

    return ListView(

        children: List.generate(

      20,

      (index) => ListTile(

        leading: Icon(Icons.access_alarm, size: 30),

        title: Text('测试$index'),

        subtitle: Text('子标题$index'),

        selected: index == 1,

        selectedColor: Colors.green,

        trailing: Icon(Icons.keyboard_arrow_right),

      ),

    ));

  }

}



class CardWidget extends StatelessWidget {

  const CardWidget({super.key});



  @override

  Widget build(BuildContext context) {

    return const Text('我是购物车数据', style: TextStyle(color: Colors.amber));

  }

}



class SettingWidget extends StatelessWidget {

  const SettingWidget({super.key});



  @override

  Widget build(BuildContext context) {

    return const Text('我是设置数据', style: TextStyle(color: Colors.green));

  }

}



  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值