美文网首页
1.底部导航栏创建

1.底部导航栏创建

作者: 冰点雨 | 来源:发表于2019-12-23 09:17 被阅读0次
ceb0b762eb875bdfbb79f0affe30651.png

入口类项目代码

import 'package:flutter/material.dart';
import './pages/index_page.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Container(
      child: MaterialApp(
        title: '百姓生活',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.pink
        ),
        home: IndexPage(),
      ),

    );
  }
}

index_page代码

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'home_page.dart';
import 'cart_page.dart';
import 'category_page.dart';
import 'member_page.dart';


class IndexPage extends StatefulWidget{
  _IndexPageState createState() => _IndexPageState();
}


class _IndexPageState extends State<IndexPage>{

  //声明一个List变量 定义底部导航的文字和图标
  final List<BottomNavigationBarItem> bottomTabs =[
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.home),
      title: Text('首页')
    ),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.search),
        title: Text('分类')
    ),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.shopping_cart),
        title: Text('购物车')
    ),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.profile_circled),
        title: Text('会员中心')
    ),
  ];


  final List tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MemberPage()
  ];

//currentIndex tabBodies的List索引,改变索引就相当于改变了页面
  //利用currentIndex得到当前选择的页面,并进行呈现出来
  int currentIndex = 0;
  var currentPage;
  @override
  void initState(){
    currentPage = tabBodies[currentIndex];
    super.initState();
  }


  @override
  Widget build(BuildContext context){
    return Scaffold(
      backgroundColor:  Color.fromRGBO(244, 245, 245, 1.0),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        items: bottomTabs,
        onTap: (index){
          setState(() {
            currentIndex = index;
            currentPage = tabBodies[currentIndex];

          });
        },
      ),
      body: currentPage,
    );
  }
}

item页以home为类


class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body:Center(
          child: Text('商城首页'),
        )
    );
  }
}

最终效果


0b56c0f9195781588c77049cad2ec0d.png

相关文章

网友评论

      本文标题:1.底部导航栏创建

      本文链接:https://www.haomeiwen.com/subject/rftznctx.html