Carmineprince's Blog
首页
    • HTML
    • CSS
    • JavaScript
    • Vue
    • React
    • TypeScript
    • Node
    • Flutter
    • Electron
    • Python
    • 运维
    • 重学前端
  • 分类
  • 标签
  • 归档

Ziqi Wang

胡思乱想程序员
首页
    • HTML
    • CSS
    • JavaScript
    • Vue
    • React
    • TypeScript
    • Node
    • Flutter
    • Electron
    • Python
    • 运维
    • 重学前端
  • 分类
  • 标签
  • 归档
  • 自定义TabBar
  • card-swiper的使用及自定义渐变导航栏
    • 添加card-swiper依赖
    • 代码
  • Flutter相关
carmineprince
2021-10-29

card-swiper的使用及自定义渐变导航栏

# card-swiper的使用及自定义渐变导航栏

# 添加card-swiper依赖

在pubspec.yaml中添加card-swiper

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  card_swiper : ^1.0.4
1
2
3
4
5

安装: 命令行输入下面的指令:

flutter packages get
1

# 代码

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

// 顶部导航栏根据滚动渐变的最大滚动距离
const APPBAR_SCROLL_OFFSET = 80;

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 轮播图图片列表
  final List _imageUrls = [
    'https://via.placeholder.com/350x150',
    'https://via.placeholder.com/350x150',
    'https://via.placeholder.com/350x150',
  ];

  // 顶部导航栏渐变透明图值
  double appBarAlpha = 0;

  // 界面滚动回调
  _onScroll(offset) {
    if (offset <= APPBAR_SCROLL_OFFSET) {
      double alpha = offset / APPBAR_SCROLL_OFFSET;
      if (alpha < 0) {
        alpha = 0;
      } else if (alpha > 1) {
        alpha = 1;
      }
      setState(() {
        appBarAlpha = alpha;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // Stack 堆叠,将navigationBar和ListView堆叠在一起,谁写在前面,谁就在底层。
      body: Stack(
        children: <Widget>[
          // 移除默认的padding
          MediaQuery.removePadding(
              // 移除的方向:顶部
              removeTop: true,
              // 上下文
              context: context,
              // 设置通知监听:监听谁就包裹谁
              child: NotificationListener(
                // 监听回调
                onNotification: (scrollNotification) {
                  // 过滤:通知是ScrollUpdateNotification类并且通知的深度来自第一层级,也就是ListView,只到这一级就可以了不再往下走。
                  if (scrollNotification is ScrollUpdateNotification &&
                      scrollNotification.depth == 0) {
                    //  从scrollNotification取出移动的像素,就是界面的偏移量
                    _onScroll(scrollNotification.metrics.pixels);
                  }
                  return false;
                },
                child: ListView(
                  children: <Widget>[
                    SizedBox(
                      height: 160.0,
                      child: Swiper(
                        // 遍历设置Swiper的Item
                        itemBuilder: (BuildContext context, int index) {
                          // BoxFit.fill 图片适应方式
                          return Image.network(_imageUrls[index],
                              fit: BoxFit.fill);
                        },
                        // item的数量(必填)
                        itemCount: _imageUrls.length,
                        // 是否自动播放
                        autoplay: true,
                        // 显示页码小圆点
                        pagination: const SwiperPagination(),
                      ),
                    ),
                    // 为了让例子界面可以滚动
                    const SizedBox(
                      height: 800,
                      child: ListTile(title: Text('haha')),
                    )
                  ],
                ),
              )),
          // Opacity包裹的组件可设置透明度
          Opacity(
            // 透明度(必填)
            opacity: appBarAlpha,
            child: Container(
              height: 80,
              decoration: const BoxDecoration(color: Colors.white),
              child: const Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Text('首页'),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
上次更新: 10/29/2021, 4:56:25 PM
自定义TabBar

← 自定义TabBar

最近更新
01
pc端rem配置
03-02
02
使用动态变量ts报错的解决
02-25
03
React Hook详解
02-18
更多文章>
Theme by Vdoing | Copyright © 2021-2022 Carmineprince | 鲁ICP备2021046263号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式