flutter - 将数据从一个屏幕传回另一个屏幕
问题描述
我是颤振的超级新手。有人可以帮我解决这个问题。我正在尝试在 2 个屏幕之间传回数据。这是第一个屏幕:
class ReportPage extends StatefulWidget{
ReportPage() : super();
@override
ReportPageState createState() => ReportPageState();
}
class ReportPageState extends State<ReportPage> {
final GlobalKey<ScaffoldState> _scaffoldstate = new GlobalKey<ScaffoldState>();
String lat;
String long;
String _name="";
String _street="";
String locality="";
String administrativeArea="";
getCurrentLocation() async{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
setState((){
lat = '${position.latitude.toString()}';
long = '${position.longitude.toString()}';
});
List<Placemark> newPlace =
await placemarkFromCoordinates(double.tryParse(lat),double.tryParse(long));
Placemark placeMark = newPlace[0];
_name = placeMark.name;
_street = placeMark.thoroughfare;
locality = placeMark.locality;
administrativeArea = placeMark.administrativeArea;
return("$_name, $locality,$_street, $administrativeArea");
}
@override
void initState() {
super.initState();
getCurrentLocation();
}
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()=> Future.value(false),
child: Scaffold(
key: _scaffoldstate,
actions:
<Widget>[
FlatButton(
textColor: Color(0xFF262AAA),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MapsDemo(plat: lat, plong: long)),
);
},
在这个屏幕中,我geolocator
用来获取lat
并将long
其传递给第二个屏幕,这就是它。
const kGoogleApiKey = "API";
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
class MapsDemo extends StatefulWidget {
MapsDemo({this.plat, this.plong}) : super();
String plat;
String plong;
final String title = "Select Location";
@override
MapsDemoState createState() => MapsDemoState();
}
class MapsDemoState extends State<MapsDemo> {
Completer<GoogleMapController> _controller = Completer();
LatLng _center;
LatLng _lastMapPosition;
Set<Marker> _markers = {
};
MapType _currentMapType = MapType.normal;
@override
void initState(){
super.initState();
_center = LatLng(double.tryParse(widget.plat), double.tryParse(widget.plong));
_lastMapPosition= LatLng(double.tryParse(widget.plat), double.tryParse(widget.plong));
}
Widget button1(IconData icon, String heroTag) {
return FloatingActionButton(
onPressed:
() async {
Prediction p = await PlacesAutocomplete.show(
context: context, apiKey: kGoogleApiKey);
displayPrediction(p);
},
heroTag: heroTag,
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Color(0xFF262AAA),
child: Icon(
icon,
size: 36.0,
),
);
}
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
setState(() {
widget.plat = lat.toString();
widget.plong = lng.toString();
}
);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: IconButton(icon: Icon(Icons.arrow_back),
color: Colors.white,
onPressed: (){
Navigator.pop(context,true);
}),
title: Text(widget.title,
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.w700, fontSize: 20,)),
backgroundColor: Color(0xFF262AAA),
),
导航后,此屏幕允许执行autocomplete search
和更改传递数据的值,即plot
和plong
。我想要的是,将plot
and的新值传递回第一个屏幕plong
。我怎么做?任何帮助将不胜感激。
解决方案
推荐阅读
- compiler-construction - 如何确定所需的别名分析类型?
- javascript - app.get 和 router.get (Express) 的区别
- vue.js - Vue:使用范围为组件的 Vuetify 库?
- python - 对于我的 if-else 语句,应该显示 else,而不是 if。我不知道为什么
- r - 试图用 R 中的 plm 在 stata 中重现 xtreg
- python - 创建给定字符串和给定字典中的所有单词的列表
- python - 用 for in 语句替换列表中的项目(Python)
- python - 无法在 pyspark 中导入 csv
- java - 为什么在条件应该为假时执行for循环?
- swift - CollectionView SizeForItemAt 基于 Cell 类?