Flutter - 路由管理 - 02 - Fluro

child: Text(‘框架 自定义 转场动画 演示’),
onPressed: () {
NavigatorUtil.gotransitionCustomDemoPage(context,
FluroConvertUtils.fluroCnParamsEncode(‘框架 自定义 转场动画 演示’));
},
),
RaisedButton(
child: Text(‘修改源码,添加使用 Flutter 的 cupertino 转场动画’),
onPressed: () {
NavigatorUtil.gotransitionCupertinoDemoPage(
context,
FluroConvertUtils.fluroCnParamsEncode(
“修改源码,添加使用 Flutter 的 cupertino 转场动画”));
},
),
],
),
);
}

/// 显示一个Dialgo
void showResultDialog(BuildContext context,String message){
showDialog(
context: context,
builder: (context) {
return new AlertDialog(
title: new Text(
“Hey Hey!”,
style: new TextStyle(
color: const Color(0xFF00D6F7),
fontFamily: “Lazer84”,
fontSize: 22.0,
),
),
content: new Text(“$message”),
actions: [
new Padding(
padding: new EdgeInsets.only(bottom: 8.0, right: 8.0),
child: new FlatButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: new Text(“OK”),
),
),
],
);
},
);
}
}

4. 场景二:传参 String,int,double,bool,自定义类型

1. 效果图

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

2. 代码

1. 注意点(类型转换 fluro_convert_util.dart)

Fluro路由地址,只能传递String类型(并且不支持中文),所以需要对 中文,int,double,bool,自定义类型进行一个转换 , 写了一个 转换类 fluro_convert_util.dart

import ‘dart:convert’;

/// fluro 参数编码解码工具类
class FluroConvertUtils {
/// fluro 传递中文参数前,先转换,fluro 不支持中文传递
static String fluroCnParamsEncode(String originalCn) {
return jsonEncode(Utf8Encoder().convert(originalCn));
}

/// fluro 传递后取出参数,解析
static String fluroCnParamsDecode(String encodeCn) {
var list = List();

///字符串解码
jsonDecode(encodeCn).forEach(list.add);
String value = Utf8Decoder().convert(list);
return value;
}

/// string 转为 int
static int string2int(String str) {
return int.parse(str);
}

/// string 转为 double
static double string2double(String str) {
return double.parse(str);
}

/// string 转为 bool
static bool string2bool(String str) {
if (str == ‘true’) {
return true;
} else {
return false;
}
}

/// object 转为 string json
static String object2string(T t) {
return fluroCnParamsEncode(jsonEncode(t));
}

/// string json 转为 map
static Map<String, dynamic> string2map(String str) {
return json.decode(fluroCnParamsDecode(str));
}
}

2. Person.dart 等下用到的自定义类型

class Person{
String name;
int age;
bool sex;

Person({this.name, this.age,this.sex});

Person.fromJson(Map<String, dynamic> json) {
name = json[‘name’];
age = json[‘age’];
sex = json[‘sex’];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data[‘name’] = this.name;
data[‘age’] = this.age;
data[‘sex’] = this.sex;
return data;
}
}

3. routes.dart

/// 配置路由地址 和 跳转类和参数handler
static String demoParams = “/deme_params”;

router.define(demoParams, handler: demoParamHandler);

4. route_handlers.dart

/// 参数传递 int ,double,bool,自定义类型
var demoParamHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List> params) {
/// params[“name”]?.first 相当于 params[“name”][0] ,打个debug 你就知道为什么了是个list
String name = params[“name”]?.first;
String age = params[“age”]?.first;
String sex = params[“sex”]?.first;
String score = params[“score”]?.first;
String personjson = params[‘personjson’]?.first;
/// 下面转换为真实想要的类型
return DemoParamsPage(
name: name,
age: FluroConvertUtils.string2int(age),
score: FluroConvertUtils.string2double(score),
sex: FluroConvertUtils.string2bool(sex),
personJson: personjson,
);
});

5. NavigatorUtil.dart

/// 跳转到 传参demo 页面
static void goDemoParamsPage(BuildContext context, String name, int age,
double score, bool sex, Person person) {
/// 对中文进行编码
String mName = FluroConvertUtils.fluroCnParamsEncode(name);
/// 对自定义类型 转为 json string
String personJson = FluroConvertUtils.object2string(person);
Application.router.navigateTo(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值