Flutter视差滚动效果插件simple_parallax的使用

Flutter视差滚动效果插件simple_parallax的使用

Pub Version Maintainer License Maintenance Flutter Status Generic badge

这个视差插件通过两种模式(容器模式和部件模式)为您的小部件添加优雅的视差效果。该插件有助于创建具有滚动效果的动态且视觉上吸引人的用户界面。

特性

  • 平滑的背景图像视差滚动。
  • 可配置的视差滚动速度。
  • 简单地集成到现有的Flutter项目中。
  • 使用provider进行状态管理,确保高效的更新。

安装

pubspec.yaml 文件中添加 simple_parallax

dependencies:
  flutter:
    sdk: flutter
  simple_parallax: ^0.1.0

^0.1.0 替换为从 pub.dev 获取的最新版本。

使用方法

simple_parallax 插件提供了两个主要的小部件来创建视差效果:

容器模式 (SimpleParallaxContainer)

容器模式用于向带有背景图像的子小部件添加视差效果。

示例
import 'package:flutter/material.dart';
import 'package:simple_parallax/simple_parallax.dart';

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

/// 应用程序演示
class MyApp extends StatelessWidget {
  /// 应用程序演示构造函数
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: SizedBox(
            //height: 300,
            //width: 600,
            child: SimpleParallaxContainer(
              //height: 300,
              imagePath: 'assets/images/background.webp', // 背景图像路径
              speed: 0.3, // 视差滚动速度
              autoSpeed: true, // 自动计算速度
              decal: 1.0, // 背景图像偏移
              child: Column(
                children: List<Widget>.generate(
                  20,
                  (int index) => Container(
                    height: 100,
                    margin: const EdgeInsets.symmetric(vertical: 10),
                    color: Colors.white.withOpacity(0.8),
                    child: Center(child: Text('Item $index')), // 列表项
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

部件模式 (SimpleParallaxWidget)

部件模式允许您在一个列表中应用视差效果到多个子部件。

示例
import 'package:flutter/material.dart';
import 'package:simple_parallax/simple_parallax.dart';

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

/// 应用程序演示
class MyApp extends StatelessWidget {
  /// 应用程序演示构造函数
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SimpleParallaxWidget(
          children: [
            Container(
              color: Colors.red,
              padding: const EdgeInsets.symmetric(vertical: 200, horizontal: 0),
              child: const SizedBox(
                height: 50,
              ),
            ),
            const SimpleParallaxItem(
              imagePath: 'assets/images/background.webp', // 背景图像路径
              height: 300,
              child: Center(
                child: Text('TEST 1'), // 子部件
              ),
            ),
            Container(
              color: Colors.greenAccent,
              child: const SizedBox(
                height: 250,
              ),
            ),
            const SimpleParallaxItem(
              imagePath: 'assets/images/background.webp', // 背景图像路径
              height: 300,
              child: Center(
                child: Text('TEST 2'), // 子部件
              ),
            ),
            Container(height: 500, color: Colors.blueGrey),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter视差滚动效果插件simple_parallax的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter视差滚动效果插件simple_parallax的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用simple_parallax插件来实现视差滚动效果的代码案例。

首先,你需要在你的pubspec.yaml文件中添加simple_parallax依赖:

dependencies:
  flutter:
    sdk: flutter
  simple_parallax: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来获取依赖。

接下来是一个完整的示例代码,展示如何使用simple_parallax来实现视差滚动效果:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Simple Parallax Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ParallaxDemoScreen(),
    );
  }
}

class ParallaxDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Simple Parallax Demo'),
      ),
      body: ParallaxScrollView(
        scrollPhysics: BouncingScrollPhysics(),
        slivers: <Widget>[
          // 背景视差层
          SliverParallaxHeader(
            height: 250.0,
            child: Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/background.jpg'), // 替换为你的背景图片
                  fit: BoxFit.cover,
                ),
              ),
            ),
            parallax: 0.5, // 控制视差效果的速度
          ),
          // 内容层
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return ListTile(
                  title: Text('Item $index'),
                );
              },
              childCount: 20,
            ),
          ),
        ],
      ),
    );
  }
}

代码说明:

  1. 添加依赖:在pubspec.yaml中添加simple_parallax依赖。
  2. 创建主应用MyApp类定义了应用的入口,设置了主题和主页。
  3. 创建主页ParallaxDemoScreen类定义了包含视差滚动效果的页面。
  4. 使用ParallaxScrollView:这是simple_parallax插件提供的主要组件,它接受一个slivers列表,用于定义不同的滚动层。
  5. 背景视差层SliverParallaxHeader定义了背景视差层,通过parallax属性控制视差效果的速度。这里设置为0.5,意味着背景层将以正常滚动速度的一半移动。
  6. 内容层SliverList定义了滚动内容,这里使用了SliverChildBuilderDelegate来动态生成列表项。

注意事项:

  • 确保你的assets/background.jpg图片存在于项目的assets文件夹中,并在pubspec.yaml中声明。
  • 根据需要调整parallax属性的值来控制视差效果的速度。

这样,你就可以在Flutter应用中实现视差滚动效果了。

回到顶部