首页 > 解决方案 > 将数据从一个屏幕传回另一个屏幕

问题描述

我是颤振的超级新手。有人可以帮我解决这个问题。我正在尝试在 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和更改传递数据的值,即plotplong。我想要的是,将plotand的新值传递回第一个屏幕plong。我怎么做?任何帮助将不胜感激。

标签: flutterdartflutter-navigation

解决方案


推荐阅读