Flutter资源

我们的项目中一般有很多图片资源,那么图片放在Flutter的什么位置呢?首先把资源复制COM + C
image.png

然后选中Flutter的根目录COM + V此时会弹出提示:
image.png
选择OK.接着来到项目的pubspecy.yaml文件, 这里不光存放图片还有今后要用的包也是在这里配置。
image.png
找到这行注释放开,注意这里一定要注意空格的格式,这里报错是因为多了两个空格,我们需要跟上面对齐
image.png
正确的写法是这样,注明图片的路径在images
image.png

安卓资源配置

在iOS中有1x/2x/3x的概念,但是在安卓中跟iOS有点不一样
image.png
image.png

底部BottomNavigationBar

1.来到main.dart的文件下,我们这里自定义MaterialApp的home小组件,其他的暂时都不用动。
2.自定义RootPage底部的点击会图片的颜色和文字的颜色,所以这里设计的就需要是一个有状态的Widget

  1. import 'package:flutter/material.dart';
  2. class RootPage extends StatefulWidget {
  3. const RootPage({Key? key}) : super(key: key);
  4. @override
  5. _RootPageState createState() => _RootPageState();
  6. }
  7. class _RootPageState extends State<RootPage> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. bottomNavigationBar: BottomNavigationBar(
  12. items: [
  13. BottomNavigationBarItem(icon: Icon(Icons.add), label: '微信'),
  14. BottomNavigationBarItem(icon: Icon(Icons.add), label: '通讯录'),
  15. BottomNavigationBarItem(icon: Icon(Icons.add), label: '发现'),
  16. BottomNavigationBarItem(icon: Icon(Icons.add), label: '我'),
  17. ],
  18. ),
  19. );
  20. }
  21. }

设置三个Item运行没有问题,但是一旦设置了四个Item�运行之后我们发现此时的页面成了这样的:
image.png
点击下面可以看到是有东西的,只是没有显示出来,这是因为这里少了一个type,给BottomNavigationBar设置type: BottomNavigationBarType.fixed�,之后运行效果出来了
image.png
我们还发现当前的BottomNavigationBar有一个currentIndex属性,默认值给的是0,我们想要它动态变化。同时这个BottomNavigationBar还有一个onTap事件,点击查看发现这里有个标记点击的是哪一个的BottomNavigationBarItem

  1. typedef ValueChanged<T> = void Function(T value);

我们把这个index传递给当前的_currentIndex,此时代码就成了这样

  1. class _RootPageState extends State<RootPage> {
  2. int _currentIndex = 0;
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. bottomNavigationBar: BottomNavigationBar(
  7. onTap: (index) {
  8. setState(() {
  9. _currentIndex = index;
  10. });
  11. },
  12. type: BottomNavigationBarType.fixed,
  13. fixedColor: Colors.green,
  14. currentIndex: _currentIndex,
  15. items: [
  16. BottomNavigationBarItem(icon: Icon(Icons.add), label: '微信'),
  17. BottomNavigationBarItem(icon: Icon(Icons.add), label: '通讯录'),
  18. BottomNavigationBarItem(icon: Icon(Icons.add), label: '发现'),
  19. BottomNavigationBarItem(icon: Icon(Icons.add), label: '我'),
  20. ],
  21. ),
  22. );
  23. }
  24. }

现在运行之后可以发现底部BarItem点击能够切换了。

对应BottomNavigationBarItemScaffold

点击下面的BarItem的时候,我们希望当前的页面的Body是对应的Scaffold的一个组件,所以此时生成四个page.dart
image.png
回到RootPage来,我们把这四个子Page装在一个数组里面,然后根据当前的_current的下标来取出做为当前的Body
image.png
此时运行效果
image.png

细节完善

  1. 点击BarItem的时候有一个水波纹的动画,这个是系统自带的我们可以去掉。

    1. theme: ThemeData(
    2. primarySwatch: Colors.blue, //整个App的主题色
    3. highlightColor: Color.fromRGBO(1, 0, 0, 0),
    4. splashColor: Color.fromRGBO(1, 0, 0, 0)
    5. ),
  2. 默认选中的时候字体有放大的效果:默认选中的字体是14,我们这里修改为12, 默认么有选中的字体是12。

    1. BottomNavigationBar(
    2. selectedFontSize: 12.0,
    3. )

    3.把图片都替换成资源里面的图片

    1. BottomNavigationBarItem(
    2. icon: Image(
    3. width: 20,
    4. height: 20,
    5. image: AssetImage('images/tabbar_chat.png')), // 正常显示
    6. activeIcon: Image(
    7. width: 20,
    8. height: 20,
    9. image: AssetImage('images/tabbar_mine_hl.png'), // 选中显示
    10. ),
    11. label: '微信'
    12. ),
  3. 把整个App的主题设置成原谅绿

    1. ThemeData(
    2. primarySwatch: Colors.green,
    3. highlightColor: Color.fromRGBO(1, 0, 0, 0),
    4. splashColor: Color.fromRGBO(1, 0, 0, 0)
    5. ),

    image.png
    好了,最初的框架已经搭建好了。