Flutter 基础总结(5) 路由的简单传值
2020-03-13 本文已影响0人
总会颠沛流离
我要你知道,这个世界上有一个人会永远等着你。无论是在什么时候,无论你在什么地方,反正你知道总会有这样一个人。
1:效果图
[图片上传中...(image.png-73b560-1584066545987-0)]
image.png
#
1:代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: "页面跳转返回的数据",
theme: ThemeData(primarySwatch: Colors.red),
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("demo"),
),
body: Center(
child: RouteButton(),
),
);
}
}
class RouteButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return RaisedButton(
onPressed: () {_navigateToNext(context);},
child: Text("go"),
);
}
_navigateToNext(BuildContext context) async {
var result =await Navigator.push(context, MaterialPageRoute(builder: (context) {
return SecondScreen();
}));
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("$result"),
));
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Second"),
),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text("1"),
onPressed:(){ Navigator.pop(context,"1");},
),
RaisedButton(
child: Text("2"),
onPressed: (){
Navigator.pop(context,"2");
},
),
],
),
),
);
}
}