Flutter引导页动画插件animated_onboarding_ns的使用

发布于 1周前 作者 htzhanglong 来自 Flutter

Flutter引导页动画插件animated_onboarding_ns的使用

animated_onboarding_ns 是一个用于实现精美引导页动画的 Flutter 插件。它提供了简单且直观的方式来创建引导页,并支持自定义页面内容和样式。

引导页示例

感谢原作者的贡献!
animated_onboarding_ns 在 pub.dev 上

使用示例

以下是一个完整的示例代码,展示如何使用 animated_onboarding_ns 创建一个带有引导页动画的应用。

示例代码

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: OnboardingScreen(),
    );
  }
}

class OnboardingScreen extends StatefulWidget {
  @override
  _OnboardingScreenState createState() => _OnboardingScreenState();
}

class _OnboardingScreenState extends State<OnboardingScreen> {
  // 定义引导页的内容
  final _pages = [
    OnboardingPage(
      child: Text(
        "标题1",
        style: TextStyle(
          color: Colors.white,
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
      color: const Color(0xffff1744), // 页面背景颜色
    ),
    OnboardingPage(
      child: Text(
        "标题2",
        style: TextStyle(
          color: Colors.white,
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
      color: const Color(0xffff9100),
    ),
    OnboardingPage(
      child: Text(
        "标题3",
        style: TextStyle(
          color: Colors.white,
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
      color: const Color(0xff00695c),
    ),
    OnboardingPage(
      child: Text(
        "标题4",
        style: TextStyle(
          color: Colors.white,
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
      color: const Color(0xff5c6bc0),
    ),
    OnboardingPage(
      child: Text(
        "标题5",
        style: TextStyle(
          color: Colors.white,
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
      color: const Color(0xff37474f),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return AnimatedOnboarding(
      pages: _pages, // 设置引导页内容
      pageController: PageController(), // 页面控制器
      onFinishedButtonTap: () {
        print("引导页结束!");
      }, // 引导页结束后的回调
      topLeftChild: Text(
        "应用名称",
        style: TextStyle(
          color: Colors.white,
          fontSize: 24,
        ),
      ), // 左上角的标题
      topRightChild: FlatButton(
        child: Text(
          "跳过",
          style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
        onPressed: () {},
      ), // 右上角的跳过按钮
    );
  }
}

代码说明

  1. 导入包

    import 'package:animated_onboarding_ns/animated_onboarding_ns.dart';

    导入 animated_onboarding_ns 包。

  2. 定义引导页内容

    final _pages = [
      OnboardingPage(
        child: Text("标题1", ...),
        color: const Color(0xffff1744),
      ),
      ...
    ];

    每个引导页由 OnboardingPage 组成,包含页面内容(如标题)和背景颜色。

  3. 构建引导页

    AnimatedOnboarding(
      pages: _pages,
      pageController: PageController(),
      onFinishedButtonTap: () { ... },
      topLeftChild: Text("应用名称", ...),
      topRightChild: FlatButton(...),
    )

更多关于Flutter引导页动画插件animated_onboarding_ns的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter引导页动画插件animated_onboarding_ns的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


animated_onboarding_ns 是一个用于在 Flutter 应用中创建引导页动画的插件。它提供了丰富的动画效果和自定义选项,使开发者能够轻松地创建吸引人的引导页。

安装

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

dependencies:
  flutter:
    sdk: flutter
  animated_onboarding_ns: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

基本用法

以下是一个简单的示例,展示如何使用 animated_onboarding_ns 创建一个引导页:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: OnboardingScreen(),
    );
  }
}

class OnboardingScreen extends StatelessWidget {
  final List<OnboardingModel> onboardingPages = [
    OnboardingModel(
      imagePath: 'assets/images/onboarding1.png',
      title: 'Welcome to App',
      description: 'This is the first page of the onboarding screen.',
    ),
    OnboardingModel(
      imagePath: 'assets/images/onboarding2.png',
      title: 'Explore Features',
      description: 'Discover all the amazing features of our app.',
    ),
    OnboardingModel(
      imagePath: 'assets/images/onboarding3.png',
      title: 'Get Started',
      description: 'Start your journey with us today!',
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedOnboarding(
        pages: onboardingPages,
        onFinish: () {
          // 引导页结束后执行的操作
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => HomeScreen()),
          );
        },
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: Text('Welcome to the Home Screen!'),
      ),
    );
  }
}

参数说明

  • pages: 一个 List<OnboardingModel>,用于定义引导页的每一页内容。OnboardingModel 包含 imagePathtitledescription 等属性。
  • onFinish: 当用户完成引导页时触发的回调函数。通常用于导航到应用的主界面。
  • indicatorColor: 页面指示器的颜色。
  • selectedIndicatorColor: 当前页面指示器的颜色。
  • skipButton: 自定义“跳过”按钮。
  • nextButton: 自定义“下一步”按钮。
  • finishButton: 自定义“完成”按钮。

自定义

你可以通过传递自定义的 skipButtonnextButtonfinishButton 来进一步定制引导页的外观和行为。例如:

AnimatedOnboarding(
  pages: onboardingPages,
  onFinish: () {
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(builder: (context) => HomeScreen()),
    );
  },
  skipButton: Text('Skip'),
  nextButton: Icon(Icons.arrow_forward),
  finishButton: Text('Get Started'),
);
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!