flutter实现类android的底部导航栏+fragment切换

在这里插入图片描述

入口:

import 'package:flutter/material.dart';

import 'MyTab.dart';

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

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home:MyTab()//只要home,占满屏幕
    );
  }
}

MyTab组件:
分为上下两部分。上面是类似于fragment,下面是导航栏。

import 'package:flutter/material.dart';
import 'package:flutter_app/HomePage.dart';
import 'package:flutter_app/MapPage.dart';
import 'package:flutter_app/SearchPage.dart';
import 'package:flutter_app/SettingsPage.dart';


class MyTab extends StatefulWidget {//继承状态可变的组件
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TabsState();
  }
}

class _TabsState extends State<MyTab> {
  int _currentIndex = 0;
  //装页面的数组
  List _pageList=[HomePage(),MapPage(),SettingsPage(),SearchPage()];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
//      appBar: AppBar(
//        title: Text("title"),
//        leading: Icon(Icons.home),
//      ),
      body: _pageList[_currentIndex],//
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        fixedColor: Colors.purple,
        onTap: (index) {//点击导航栏的item
          setState(() {//改变状态
            _currentIndex = index;
          });
        },
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("主页")),
          BottomNavigationBarItem(icon: Icon(Icons.map), title: Text("地图")),
          BottomNavigationBarItem(icon: Icon(Icons.settings), title: Text("设置")),
          BottomNavigationBarItem(icon: Icon(Icons.search), title: Text("搜索")),
        ],
      ),
    );
  }
}

HomePage组件:

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("home"),
          leading: Icon(Icons.home),
        ),
        body: ListView(
          children: <Widget>[
            _buildChild(),
            _buildChild(),
            _buildChild(),
            _buildChild(),
            _buildChild(),
            _buildChild(),
          ],
        ),
      ),
    );
  }

  Widget _buildChild() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Image.network("https://www.itying.com/images/flutter/1.png"),
    );
  }
}

MapPage组件:

import 'package:flutter/material.dart';

class MapPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("map"),
          leading: Icon(Icons.map),
        ),
        body: ListView(
          children: <Widget>[
            _buildChild(),
            _buildChild(),
            _buildChild(),
            _buildChild(),
            _buildChild(),
            _buildChild(),
          ],
        ),
      ),
    );
  }

  Widget _buildChild() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Image.network("https://www.itying.com/images/flutter/2.png"),
    );
  }
}

另外的两个page页面,类似。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值