当前位置:网站首页>The flyer realizes page Jump through routing routes

The flyer realizes page Jump through routing routes

2022-04-23 19:10:00 Little brother

1. Page Jump , First, build a route to manage the jump page Routers.dart

import 'package:flutter_app/Login.dart';
import 'package:flutter_app/main.dart';

// Define jump page usage 
class Routers {
  static String root = ""; // The following page must be defined as this style 
  static String login = "/Login";

  static final routers = {
    root: (context) => Splash(),
    login: (context) => Login(),
  };
}

Here I have prepared two pages ,Splash and Login , What I have to do is from Splash The page jumps to Login page .

2. Here we mainly look at the before jump Splash What is done on the page .

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_app/Utils/Routers.dart';

void main() =>runApp( Splash());

class Splash extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
         initialRoute: "/",
         routes: Routers.routers,// Route jump uses 
        // home: HomePage()
    );
  }
}

  @override
  void initState() {
    super.initState();
    startTime();
  }

  void startTime() {
// Time delay 5000 Execution in milliseconds 
    Future.delayed(const Duration(milliseconds: 5000), () {
      // Delayed code 
    Navigator.of(context).pushReplacementNamed('/Login');// Page to jump to 
    });
  }
}

 notes :  First, reference the routing tool class  import 'package:flutter_app/Utils/Routers.dart';
 The initialization route here  initialRoute: "/"  and  Home  It can't be used at the same time , Will conflict and report errors .

I made a delayed automatic jump function of the page , Used here 2 Function code in :

① Delay processing method :

// Time delay 5000 Execution in milliseconds

Future.delayed(const Duration(milliseconds: 5000), () {

// Delayed code

});

② Page Jump method :

Navigator.of(context).pushReplacementNamed('/Login');// Page to jump to

Here, the page jump function is realized , Daily records .

版权声明
本文为[Little brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210601422422.html