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(