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(
context,
Routes.demoParams +
“?name=KaTeX parse error: Expected 'EOF', got '&' at position 5: name&̲age=age&score=KaTeX parse error: Expected 'EOF', got '&' at position 6: score&̲sex=sex&personjson=$personJson”);
}

6. home_page.dart 跳转按钮

String name = “来自第一个界面测试一下”;
int age = 14;
double score = 6.4;
bool sex = true;
Person person = new Person(name: ‘Zeking’, age: 18, sex: true);

RaisedButton(
child: Text(‘传递参数string ,int,double,bool ,自定义类型’),
onPressed: () {
NavigatorUtil.goDemoParamsPage(
context, name, age, score, sex, person);
},
),

7. demo_params_pag.dart

class DemoParamsPage extends StatefulWidget {
final String name;
final int age;
final double score;
final bool sex;
final String personJson;

DemoParamsPage({this.name, this.age, this.score, this.sex, this.personJson});

@override
_DemoParamsPageState createState() => _DemoParamsPageState();
}

class _DemoParamsPageState extends State {
@override
Widget build(BuildContext context) {
/// 对 中文 进行解码
String mName = FluroConvertUtils.fluroCnParamsDecode(widget.name);
/// 对自定义类 进行解析
Person person =
Person.fromJson(FluroConvertUtils.string2map(widget.personJson));
print(person.name);
print(person.age);
print(person.sex);
/// 下面的写法也可以
Map<String, dynamic> data = FluroConvertUtils.string2map(widget.personJson);
print(data[“name”]);
print(data[“age”]);
print(data[“sex”]);

return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(‘name: m N a m e ′ ) , T e x t ( ′ a g e : mName'), Text('age: mName),Text(age{widget.age}’),
Text(‘score: w i d g e t . s c o r e ′ ) , T e x t ( ′ s e x : {widget.score}'), Text('sex: widget.score),Text(sex{widget.sex}’),
Text(‘Person:${person.toJson().toString()}’),
RaisedButton(
child: Text(‘返回’),
onPressed: () {
NavigatorUtil.goBack(context);
},
)
],
),
),
);
}
}

5. 场景三:接收返回值 String,int,double,自定义类型

1. 效果图

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

2. routes.dart

static String returnParams = “/return_params”;

router.define(returnParams, handler: returnParamHandler);

3. route_handlers.dart

/// 关闭页面,返回参数
var returnParamHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List> params) {
return ReturnParamsPage();
});

4. NavigatorUtil.dart

/// 跳转到 会返回参数的 页面
static Future goReturnParamsPage(BuildContext context) {
return Application.router.navigateTo(context, Routes.returnParams);
}

5. home_page.dart

RaisedButton(
child: Text(‘传递参数,接受返回值’),
onPressed: () {
NavigatorUtil.goReturnParamsPage(context).then((result) {
debugPrint(‘KaTeX parse error: Expected '}', got 'EOF' at end of input: …); debugPrint('{result.toJson().toString()}’);
} else {
message = ‘ r e s u l t ′ ; d e b u g P r i n t ( ′ result'; debugPrint(' result;debugPrint(result’);
}
showResultDialog(context, message);
});
},
)

/// 显示一个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”),
),
),
],
);
},
);
}

6. return_params_page.dart

class ReturnParamsPage extends StatefulWidget {
@override
_ReturnParamsPageState createState() => _ReturnParamsPageState();
}

class _ReturnParamsPageState extends State {
@override
Widget build(BuildContext context) {
Person person = new Person(name: “returnName”, age: 23, sex: false);
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: RaisedButton(
child: Text(‘返回,并且返回string’),
onPressed: () {
NavigatorUtil.goBackWithParams(context, “我是返回值哦”);
},
),
),
RaisedButton(
child: Text(‘返回,并且返回int’),
onPressed: () {
NavigatorUtil.goBackWithParams(context, 12);
},
),
RaisedButton(
child: Text(‘返回,并且返回double’),
onPressed: () {
NavigatorUtil.goBackWithParams(context, 3.1415926);
},
),
RaisedButton(
child: Text(‘返回,并且返回bool’),
onPressed: () {
NavigatorUtil.goBackWithParams(context, true);
},
),
RaisedButton(
child: Text(‘返回,并且返回自定义类型’),
onPressed: () {
NavigatorUtil.goBackWithParams(context, person);
},
)
],
),
);
}
}

6. 场景四:使用 框架 自带 的 转场动画

1. 效果图

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

2. routes.dart

static String transitionDemo = “/transitionDemo”;

router.define(transitionDemo, handler: transitionDemoHandler);

3. route_handlers.dart

/// 转场动画 页面
var transitionDemoHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List> params) {
String title = params[“title”]?.first;
return TransitionDemoPage(title);
});

4. NavigatorUtil.dart

/// 跳转到 转场动画 页面 , 这边只展示 inFromLeft ,剩下的自己去尝试下,
/// 框架自带的有 native,nativeModal,inFromLeft,inFromRight,inFromBottom,fadeIn,custom
static Future gotransitionDemoPage(BuildContext context, String title) {
return Application.router.navigateTo(
context, Routes.transitionDemo + “?title=$title”,
/// 指定了 转场动画 inFromLeft
transition: TransitionType.inFromLeft);
}

5. home_page.dart

RaisedButton(
child: Text(‘框架 自带 转场动画 演示’),
onPressed: () {
NavigatorUtil.gotransitionDemoPage(context,
/// 这边进行了 String 编码
FluroConvertUtils.fluroCnParamsEncode("框架 自带 转场动画 演示 \n\n\n "
"这边只展示 inFromLeft ,剩下的自己去尝试下,\n\n\n "
“架自带的有 native,nativeModal,inFromLeft,inFromRight,inFromBottom,fadeIn,custom”));
},
),

6. transition_demo_page.dart

场景五 ,场景六 ,用到一样的 transition_demo_page 后面就不展示了

class TransitionDemoPage extends StatefulWidget {
final String title;

TransitionDemoPage(this.title);

@override
_TransitionDemoPageState createState() => _TransitionDemoPageState();
}

class _TransitionDemoPageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Text(
/// string 解码
FluroConvertUtils.fluroCnParamsDecode(widget.title),
textAlign: TextAlign.center,
)),
RaisedButton(
child: Text(‘返回’),
onPressed: () {
NavigatorUtil.goBack(context);
},
)
],
),
);
}
}

7. 场景五:自定义转场动画

1. 效果图

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

2. routes.dart

static String transitionCustomDemo = “/transitionCustomDemo”;

router.define(transitionCustomDemo, handler: transitionDemoHandler);

3. route_handlers.dart

/// 转场动画 页面
var transitionDemoHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List> params) {
String title = params[“title”]?.first;
return TransitionDemoPage(title);
});

4. NavigatorUtil.dart

/// 自定义 转场动画
static Future gotransitionCustomDemoPage(BuildContext context, String title) {
var transition = (BuildContext context, Animation animation,
Animation secondaryAnimation, Widget child) {
return new ScaleTransition(
scale: animation,
child: new RotationTransition(
turns: animation,
child: child,
),
);
};
return Application.router.navigateTo(
context, Routes.transitionCustomDemo + “?title=$title”,
transition: TransitionType.custom, /// 指定是自定义动画
transitionBuilder: transition, /// 自定义的动画
transitionDuration: const Duration(milliseconds: 600)); /// 时间
}

5. home_page.dart

RaisedButton(
child: Text(‘框架 自定义 转场动画 演示’),
onPressed: () {
NavigatorUtil.gotransitionCustomDemoPage(context,
FluroConvertUtils.fluroCnParamsEncode(‘框架 自定义 转场动画 演示’));
},
),

8. 场景六:修改源码,添加使用 Flutter 的 cupertino 转场动画

1. 查看源码,为什么没有 Flutter 的 cupertino

看下图,发现它自带的 转场动画 只有 这几个,没有我喜欢的Fluttercupertino转场动画,侧滑关闭页面,

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

继续看源码 ,看下图,最后也是调用了系统的MaterialPageRoutePageRouteBuilder

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

2. 修改源码

看下图,自己添加 cupertino 类型

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

看下图,添加CupertinoPageRoute

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

3. 效果图

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

4. routes.dart

static String transitionCupertinoDemo = “/transitionCupertinoDemo”;

router.define(transitionCupertinoDemo, handler: transitionDemoHandler);

5. route_handlers.dart

/// 转场动画 页面
var transitionDemoHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List> params) {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
厂,18年进入阿里一直到现在。**

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-J0BsDtE1-1715511976122)]

[外链图片转存中…(img-YQPcUIPG-1715511976123)]

[外链图片转存中…(img-DTrekzUA-1715511976123)]

[外链图片转存中…(img-ta3FSXtU-1715511976124)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值