[Flutter] Using Fluro to implement Flutter routing management

I. Introduction

In the development of Flutter, routing management is a very important part, which is responsible for the jump and data transmission between pages. As a powerful Flutter routing library, Fluro provides many flexible routing options, such as wildcards, named parameters, and clear routing definitions, making routing management more convenient and efficient.

The goal of this article is to help you master the following:

  • Understand the basic concepts and functions of Fluro
  • Learn how to install and configure Fluro
  • Master the basic use of Fluro, including route definition, navigation, parameter passing and receiving, and route conversion

This is the blog published by Xiaoyu Youth on CSDN in 2023. Due to the rampant copyright infringement of the collection station, if you do not see this article on CSDN, please contact me through CSDN, thank you for your support~

Are you eager to become a Flutter expert with more tips and best practices? We have great news for you! Flutter from zero to one basic entry to the application line of the whole strategy is waiting for you to join! This column contains all the resources you need to learn Flutter, including code samples and in-depth analysis. The content of the column will continue to be updated, and the price will increase accordingly. Join now and enjoy the best price! In addition, we also have a dedicated discussion group, you can click here to join our discussion group to communicate and learn with other Flutter learners. Let's start our Flutter learning journey today!

image-20230703142533573

2. Version information

Before we start, we need to make sure our development environment meets the following requirements:

  • Flutter version: 3.10.0 or higher
  • Dart SDK version: 3.0.0 or higher
  • Fluro version: 2.0.5

Please make sure your Flutter and Dart SDK versions meet the above requirements. If your version is lower than these requirements, you may need to update your Flutter and Dart SDKs.

3. Introduction to Fluro

Fluro is a routing library designed for Flutter that provides many flexible routing options such as wildcards, named parameters, and clear routing definitions. These functions allow us to manage and use routing more conveniently.

Key features of Fluro include:

  1. Easy Route Navigation: You can easily define and navigate to new routes.
  2. Function handlers: You can map a route to a function instead of a route.
  3. Wildcard parameter matching: You can use wildcards to match any number and type of parameters.
  4. Query string parameter parsing: You can parse query string parameters and pass them to your route handler.
  5. Built-in common transitions: Fluro provides some common routing transitions, such as fade in and fade out, slide in and slide out, etc.

4. Installation and configuration of Fluro

Before we can start using Fluro, we first need to add it to our project. You can complete this process by following these steps:

  1. Open your pubspec.yamlfile, and add dependenciesunder fluro, like so:
dependencies:
  flutter:
    sdk: flutter

  fluro: ^2.0.5
  1. Save your pubspec.yamlfile, then run the command in a terminal flutter packages getto get the package.

  2. In your Dart file, you can import Fluro by:

import 'package:fluro/fluro.dart';

5. Basic use: Routing definition and navigation of Fluro

Routing definitions and navigation are very simple in Fluro. First, we need to create a FluroRouter object, and then use this object to define our route and route processor.

// 创建 FluroRouter 对象
final router = FluroRouter();

// 定义路由处理器
var usersHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    
    
  return UsersScreen(params["id"][0]);
});

// 定义路由
void defineRoutes(FluroRouter router) {
    
    
  router.define("/users/:id", handler: usersHandler);
}

In this example, we define a route called "/users/:id", and when we navigate to this route, the handler is invoked, passing the route usersHandlerparameters to UsersScreen.

Next, we can navigate to the route we just defined by:

router.navigateTo(context, "/users/1234", transition: TransitionType.fadeIn);

6. Basic use: Fluro parameter passing and receiving

In Fluro, we can pass data through routing parameters. These parameters can be defined directly in the route string, or passed through the query string.

// 定义路由处理器
var usersHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    
    
  // 获取路由参数
  String id = params["id"][0];
  return UsersScreen(id);
});

// 定义路由
void defineRoutes(FluroRouter router) {
    
    
  router.define("/users/:id", handler: usersHandler);
}

// 导航到路由,并传递参数
router.navigateTo(context, "/users/1234", transition: TransitionType.fadeIn);

In this example, we specified a parameter called "id" when defining the route. Then, when navigating to the route, we passed the value of this parameter in the route string. Finally, in the route handler, we get the value of this parameter and pass it to the UsersScreen.

7. Basic usage: Routing conversion of Fluro

Fluro provides some built-in routing transitions like fades, slides in and slide out, etc. You can specify the transformation type when defining a route, or when navigating to a route.

// 定义路由处理器
var usersHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    
    
  return UsersScreen(params["id"][0]);
});

// 定义路由,并指定转换类型
void defineRoutes(FluroRouter router) {
    
    
  router.define("/users/:id", handler: usersHandler, transitionType: TransitionType.inFromLeft);
}

// 导航到路由,并指定转换类型
router.navigateTo(context, "/users/1234", transition: TransitionType.fadeIn);

In this example, we specified the transition type as when defining the route TransitionType.inFromLeft, which means that when we navigate to this route, the new page will slide in from the left. Then, when navigating to the route, we specified the transition type as TransitionType.fadeIn, which means that the new page will appear in a fade-in manner.

8. Summary

In this article, we learned how to use Fluro for route management. We learned about the basic concepts and functions of Fluro, learned how to install and configure Fluro, and mastered the basic use of Fluro, including routing definition, navigation, parameter passing and receiving, and routing conversion.

By studying this article, you should be able to use Fluro for basic routing management in your project. However, Fluro is much more than that, providing many advanced features like wildcard parameter matching, query string parameter parsing, custom route transformations, and more. If you want to learn more about Fluro, I recommend you to read our next article, where we will dive into the advanced use of Fluro.

This is the blog published by Xiaoyu Youth on CSDN in 2023. Due to the rampant copyright infringement of the collection station, if you do not see this article on CSDN, please contact me through CSDN, thank you for your support~

Are you curious about Flutter and want to learn more about it? Then, Flutter from zero to one basic introduction to application launch guide will be your best choice! Here, you can find comprehensive Flutter learning resources, including code samples and in-depth analysis. Are you wondering how to build apps with Flutter? All the answers are in our column! Don't hesitate anymore, the content of the column will continue to be updated, and the price will increase accordingly. Join now and enjoy the best price! Let's explore the world of Flutter together! Want to know more? Click here to view the Flutter Developer 101 Getting Started booklet column guide . In addition, we also have a dedicated discussion group, you can click here to join our discussion group to communicate and learn with other Flutter learners.

Guess you like

Origin blog.csdn.net/diandianxiyu/article/details/131515340