How to pass data from one class to another?

37 views
Skip to first unread message

sona

unread,
Apr 21, 2021, 6:47:17 AM4/21/21
to Flutter Development (flutter-dev)

How to pass myLocation.latitude of class SelectonMap to textfield of FormPage

class SelectOnMap extends StatefulWidget {

static final String pageName = "selectonmappage";

Position currentposition;
SelectOnMap(this.currentposition);

@override
State<StatefulWidget> createState() {
return SelectOnMapState(this.currentposition);
}

}



class SelectOnMapState extends State<SelectOnMap>
with TickerProviderStateMixin {

Position currentposition;
//String _currentAddress;
SelectOnMapState(this.currentposition);

static LatLng myLocation = new LatLng(51.5, -0.09);
@override
void initState() {
super.initState();
setState(() {
myLocation;
});
}
@override
Widget build(BuildContext context) {

Size screenSize = MediaQuery.of(context).size;


child: Scaffold(

body:Container(
height : MediaQuery.of(context).size.height,
child:new Directionality(
textDirection: TextDirection.rtl,
child: new Container(
padding: new EdgeInsets.only(bottom: 0.0, left: 1.0, right: 1.0),
color: Colors.white,
child: new FlutterMap(
options: new MapOptions(
center: LatLng(currentposition.latitude,currentposition.longitude),

zoom: 15.0,
maxZoom: 15.0,
minZoom: 3.0,
onTap: _handleTap
),
layers: [
new TileLayerOptions(
urlTemplate:
"https://{s}.a b c",
subdomains: ['a', 'b', 'c'],
//tileProvider: NonCachingNetworkTileProvider(),
),
new MarkerLayerOptions(
markers:[
Marker(
width: 80.0,
height: 80.0,
point: myLocation,

builder: (ctx) =>
new Container(

child: IconButton(icon: Icon(Icons.location_on,
color: Colors.green,
), onPressed: null),
),
),
]
)
],
)
)
),
),
floatingActionButton: FloatingActionButton(
onPressed: (){ Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FormPage( :myLocation.latitude),
));
},

child: Icon(Icons.check),
backgroundColor: Colors.blue,
),

);
}
_handleTap(LatLng point) {
setState(() {
myLocation = point;
print("hello");
print(myLocation.longitude);
print(myLocation.latitude);
//_getAddressFromLatLng();
});
}
void getLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position.latitude);
print("lat");
print(position.longitude);
}

void moveToLastScreen() {
Navigator.of(context).pop(

);
}

}
import 'dart:convert';
import 'location.dart';
import 'package:flutter/material.dart';
import 'InputDeco_design.dart';
import 'package:http/http.dart' as http;

class FormPage extends StatefulWidget {


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

class _FormPageState extends State<FormPage> {


String stopname, platname, platdesc,direction, lat,lng,stop_name;

//TextController to read text entered in text field
TextEditingController _stopname = TextEditingController();
TextEditingController _platname = TextEditingController();
TextEditingController _platdesc = TextEditingController();
TextEditingController _direction = TextEditingController();
TextEditingController _lat = TextEditingController();
TextEditingController _lng = TextEditingController();

final GlobalKey<FormState> _formkey = GlobalKey<FormState>();


@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
radius: 30,
backgroundColor: Colors.black,
child: Image.asset(
'assets/itspe.png', height: 50, width: 45,),
),
SizedBox(
height: 15,
),
Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(
controller:_stopname,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.person, "Stop Name"),
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter a Stop Name';
}
return null;
},
onSaved: (String value) {
stopname = value;
},
),
),
Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(
controller:_platname,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(
Icons.email, "Platform Name"),
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter a Platform Name';
}

return null;
},
onSaved: (String value) {
platname = value;
},
),
),

Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(
controller:_platdesc,
keyboardType: TextInputType.number,
decoration: buildInputDecoration(
Icons.phone, "Platform Description"),
validator: (String value) {
if (value.isEmpty) {
return 'Please enter a Description ';
}
return null;
},
onSaved: (String value) {

},
),
),
Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: _direction,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.lock, "Towards"),
validator: (String value) {
if (value.isEmpty) {
return 'Please enter Towards';
}
return null;
},
onSaved: (String value) {
direction= value;
},

),
),
Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(

controller: _lat,

keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.lock, "Latitude",),
validator: (String value) {
if (value.isEmpty) {
return 'Please enter Latitude';
}

return null;
},
onSaved: (String value) {
lat = value;
},

),
),
Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: _lng,


keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.lock, "Longitude"),
validator: (String value) {

if (value.isEmpty) {
return 'Please enter Longitude';
}


return null;
},
onSaved: (String value) {
lng = value;
},

),
),

SizedBox(
width: 200,
height: 50,
child: RaisedButton(
color: Colors.redAccent,
onPressed: () {
if (_formkey.currentState.validate()) {
RegistrationUser();
print("successful");

return;
} else {
print("UnSuccessfull");
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(60.0),
side: BorderSide(color: Colors.blue, width: 2)
),
textColor: Colors.white,
child: Text("Submit"),

),
)
],
),
),
),
),
);
}

Marijo Mihelčić

unread,
Apr 22, 2021, 5:07:41 AM4/22/21
to Flutter Development (flutter-dev)
Something like this:


class FormPage extends StatefulWidget {
final LatLng myLocation;

FormPage({Key? key, required this.myLocation   }) : super(key: key);


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

class _FormPageState extends State<FormPage> {
@override
  Widget build(BuildContext context) {
     return Text(widget.myLocation.toString());
Reply all
Reply to author
Forward
0 new messages