Advertisement

Flutter 闪屏页(开屏页)

阅读量:

Flutter 闪屏页(开屏页)

首先阐述我们希望达成的目标。具体而言,在三秒钟自动切换至主页面,并设计一个启动页(opening page)。该启动页具备如下特性:当用户在该界面触控时应立即跳过此启动页。当返回至主界面时系统应直接终止程序而无需返回至启动页。

创建images文件夹以存储图片,并确保它位于根目录下。
为图片配置路径设置。
在main.dart中实现开屏页面的启动。
开发并实现开屏页面的功能。
这部分具体内容可由开发者自行设计与实现。

images文件夹

(2)为图片配置路径

获取pubspec.yaml文件的位置。接下来,在Flutter插件设置中指定图片路径位置。系统会在此处自动生成相关注释信息。通过参考这些提示信息来正确设置图片路径位置。

复制代码
    # To add assets to your application, add an assets section, like this:
      # assets:
      #  - images/a_dot_burr.jpeg
      #  - images/a_dot_ham.jpeg
    
      
      
      
      
    
    AI写代码
复制代码
    flutter:
    
      # The following line ensures that the Material Icons font is
      # included with your application, so that you can use the icons in
      # the material Icons class.
      uses-material-design: true
      # To add assets to your application, add an assets section, like this:
      # assets:
      #  - images/a_dot_burr.jpeg
      #  - images/a_dot_ham.jpeg
      assets:
    - images/splash_img.jpg
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

(3)编写main.dart让其开启第一个页面为开屏页面

复制代码
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'FlutterDemo',
      routes: <String,WidgetBuilder>{//配置路径
        '/HomePage':(BuildContext context)  => new HomePage(),
      },
      theme: new ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in IntelliJ). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: new SplashPage()
    );
      }
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

(4)编写开屏页面

复制代码
    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:flutter_demo/view/HomePage.dart';
    
    class SplashPage extends StatefulWidget{
    
      SplashPage({Key key}):super(key:key);
      @override
      _SplashPage createState()=> new _SplashPage();
    
    }
    
    class _SplashPage extends State<SplashPage>{
    
      bool isStartHomePage = false;
    
      @override
      Widget build(BuildContext context) {
    // TODO: implement build
    return new GestureDetector(
      onTap: goToHomePage,//设置页面点击事件
      child: Image.asset("images/splash_img.jpg",fit: BoxFit.cover,),//此处fit: BoxFit.cover用于拉伸图片,使图片铺满全屏
    );
      }
    
      //页面初始化状态的方法
      @override
      void initState() {
    super.initState();
    //开启倒计时
    countDown();
      }
    
      void countDown() {
    //设置倒计时三秒后执行跳转方法
    var duration = new Duration(seconds: 3);
    new Future.delayed(duration, goToHomePage);
      }
    
      void goToHomePage(){
    //如果页面还未跳转过则跳转页面
    if(!isStartHomePage){
      //跳转主页 且销毁当前页面
      Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context)=>new HomePage()), (Route<dynamic> rout)=>false);
      isStartHomePage=true;
    }
      }
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

(5)编写主页面,这里我也就不写了,主页面想是什么样自己随便写即可

写在末尾

至此我们的开屏页面已完成。如果你使用的图片并非本地文件,则还可以调用Image.network(src)的方法来实现加载网络图片的功能。


全部评论 (0)

还没有任何评论哟~