Flutter启动页插件easy_splash的使用

Flutter启动页插件easy_splash的使用

EasySplash

EasySplash 是一个用于简化 Flutter 应用程序启动页创建的插件。通过 EasySplash,开发者可以轻松地集成吸引人的启动页,从而提升初始用户体验,而无需手动实现的复杂性。

特性

  • 流线型集成:使用少量代码即可创建启动页,减少开发时间和复杂度。
  • 自定义选项:可以配置持续时间、图像类型(本地资源或网络)和图像资源。
  • 布局灵活性:可以自定义填充和位置,以创建视觉上吸引人的启动页。
  • 支持本地和网络图像:无缝显示本地资源和网络图像。
  • 平滑过渡:确保从启动页到应用程序主内容的平滑过渡。

安装

pubspec.yaml 文件中添加依赖项:

flutter pub add easy_splash

使用示例

以下是一个完整的示例,展示如何使用 EasySplash 插件来创建启动页。

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 这个小部件是你的应用的根。
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const EasySplash(
          seconds: 2, // 持续时间2秒
          nextScreen: MyHomePage(title: "Example App"), // 下一个页面
          indicatorHeight: 15, // 加载指示器的高度
          imgType: ImgType.assetImage, // 图像类型为本地资源
          image: "assets/logo.png", // 本地资源路径
          hasIndicator: true), // 是否显示加载指示器
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              '你已经按下了按钮多少次:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '增加',
        child: const Icon(Icons.add),
      ),
    );
  }
}

说明

  1. 导入包

    import 'package:flutter/material.dart';
    import 'package:easy_splash/easy_splash.dart';
    
  2. 主应用

    void main() {
      runApp(const MyApp());
    }
    
  3. 启动页配置

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
          home: const EasySplash(
              seconds: 2,
              nextScreen: MyHomePage(title: "Example App"),
              indicatorHeight: 15,
              imgType: ImgType.assetImage,
              image: "assets/logo.png",
              hasIndicator: true),
        );
      }
    }
    
  4. 主页面

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text(
                  '你已经按下了按钮多少次:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: '增加',
            child: const Icon(Icons.add),
          ),
        );
      }
    }
    

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

1 回复

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


当然,以下是一个关于如何在Flutter项目中使用easy_splash插件来实现启动页的示例代码。easy_splash是一个非常方便的插件,用于创建和管理Flutter应用的启动页。

首先,确保你的Flutter项目已经创建完毕,并且已经在pubspec.yaml文件中添加了easy_splash依赖:

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

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

接下来,你需要按照以下步骤配置和使用easy_splash插件。

1. 配置easy_splash资源

assets文件夹下创建splash.png作为你的启动页图片。然后在pubspec.yaml中声明这个资源:

flutter:
  assets:
    - assets/splash.png

2. 创建easy_splash配置文件

在项目的根目录下创建一个名为assets/easy_splash.yaml的配置文件,并添加如下内容:

image: assets/splash.png
background_color: "#FFFFFF"  # 背景颜色,可以根据需要调整
animate_logo: true  # 是否启用logo动画(如果图片包含可动画的部分)
duration: 3000  # 启动页显示时长,单位为毫秒
navigate_after_seconds: 3  # 自动导航到主页的时长,单位为秒(可选,如果设置了duration则这个参数会被忽略)
routes:
  "/": "/home"  # 当启动页结束后导航到的路由路径

3. 初始化easy_splash

main.dart文件中,使用EasySplash来初始化你的应用:

import 'package:flutter/material.dart';
import 'package:easy_splash/easy_splash.dart';
import 'home_screen.dart';  // 假设你有一个home_screen.dart文件作为主页

void main() {
  runApp(EasySplash(
    title: 'My App',  // 应用名称(可选)
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(
        builder: (context) {
          // 在这里获取EasySplash的状态并决定是否显示启动页
          final EasySplashController controller = EasySplash.of(context);
          return controller.splashScreenVisible
              ? Container()  // 启动页显示时,这里可以返回一个空容器或加载指示器
              : HomeScreen();  // 启动页结束后,导航到主页
        },
      ),
      routes: {
        '/home': (context) => HomeScreen(),
      },
    );
  }
}

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

4. 运行应用

现在你可以运行你的Flutter应用,应该会看到启动页显示指定的图片和配置的背景颜色,并在设定的时间后自动跳转到主页。

这个示例展示了如何使用easy_splash插件来创建一个简单的启动页。你可以根据需要进一步自定义和扩展这个启动页,比如添加更复杂的动画效果或者处理不同的路由逻辑。

回到顶部