Flutter组件学习(21)实现app首页

Step 1:新建工程,添加导航栏

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return AppWidget();
  }
}
class AppWidget extends State{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.blue,
          title: Text(
            '首页',
            style: TextStyle(
              color: Colors.white,
              fontSize: 18,
            ),
          ),
        ),
      ),
    );
  }
}

 Step 2:添加悬浮按钮FloatingActionButton

floatingActionButton: FloatingActionButton(
  child: Icon(Icons.add),
  backgroundColor: Colors.blue,
),

 Step 3:添加底部导航BottomNavigationBar

items    BottomNavigationBarItem类型的List    底部导航栏的显示项
onTap    ValueChanged < int >    点击导航栏子项时的回调
currentIndex    当前显示项的下标
fixedColor    底部导航栏type为fixed时导航栏的颜色,如果为空的话默认使用ThemeData.primaryColor
iconSize    BottomNavigationBarItem icon的大小
type

有fixed和shifting两个类型,显示效果不一样

BottomNavigationBarType.shifting


BottomNavigationBarType.fixed

bottomNavigationBar: BottomNavigationBar(
  fixedColor: Colors.blue,
  currentIndex: _selectedIndex,
  onTap: _onItemTapped,
  items: <BottomNavigationBarItem>[
    BottomNavigationBarItem(
        icon:Icon(Icons.home,color: Colors.black,),
      title: Text('首页',style: TextStyle(color: Colors.black),)
    ),

    BottomNavigationBarItem(
        icon:Icon(Icons.search,color: Colors.black,),
        title: Text('探索',style: TextStyle(color: Colors.black),)
    ),

    BottomNavigationBarItem(
        icon:Icon(Icons.message,color: Colors.black,),
        title: Text('消息',style: TextStyle(color: Colors.black),)
    ),

    BottomNavigationBarItem(
        icon:Icon(Icons.people,color: Colors.black,),
        title: Text('个人中心',style: TextStyle(color: Colors.black),)
    ),
  ],
)

Step4:创建4个Tab页

 

class TabOne extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Text(
        '首页',
        style: TextStyle(
          fontSize: 40,
          color: Colors.blue
        ),
      ),
    );
  }
}

全部代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return AppWidget();
  }
}

class AppWidget extends State{

  var pages = [TabOne(),TabTwo(),TabThree(),TabFour()];

  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.blue,
          title: Text(
            '首页',
            style: TextStyle(
              color: Colors.white,
              fontSize: 18,
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          backgroundColor: Colors.blue,
        ),

        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          fixedColor: Colors.blue,
          currentIndex: _selectedIndex,
          onTap: _onItemTapped,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon:Icon(Icons.home,color: Colors.black,),
              title: Text('首页',style: TextStyle(color: Colors.black),)
            ),

            BottomNavigationBarItem(
                icon:Icon(Icons.search,color: Colors.black,),
                title: Text('探索',style: TextStyle(color: Colors.black),)
            ),

            BottomNavigationBarItem(
                icon:Icon(Icons.message,color: Colors.black,),
                title: Text('消息',style: TextStyle(color: Colors.black),)
            ),

            BottomNavigationBarItem(
                icon:Icon(Icons.people,color: Colors.black,),
                title: Text('个人中心',style: TextStyle(color: Colors.black),)
            ),
          ],
        ),

        body: pages[_selectedIndex],
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

}

class TabOne extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Text(
        '首页',
        style: TextStyle(
          fontSize: 40,
          color: Colors.blue
        ),
      ),
    );
  }
}

class TabTwo extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Text(
        '探索',
        style: TextStyle(
            fontSize: 40,
            color: Colors.blue
        ),
      ),
    );
  }
}

class TabThree extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Text(
        '消息',
        style: TextStyle(
            fontSize: 40,
            color: Colors.blue
        ),
      ),
    );
  }
}

class TabFour extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Text(
        '我的',
        style: TextStyle(
            fontSize: 40,
            color: Colors.blue
        ),
      ),
    );
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小苏的小小苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值