how to pass textformfield data to api?

457 views
Skip to first unread message

sona

unread,
Apr 22, 2021, 5:50:20 AM4/22/21
to Flutter Development (flutter-dev)
import 'dart:convert';

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


class FormPage extends StatefulWidget {
final String text, text2;
FormPage({Key key, @required this.text,@required this.text2}) : super(key: key);
@override
_FormPageState createState() => _FormPageState(this.text,this.text2);
}

class _FormPageState extends State<FormPage> {
String text,text2;
_FormPageState(this.text,this.text2);
//String stopname, platname, platdesc,direction, lat,lng,stop_name;

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/it.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 Plat 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, "Plat Des"),
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:TextEditingController.fromValue(
TextEditingValue(
text: text,
// selection: TextSelection.collapsed(offset: text.length),
),
),
),
),

Padding(
padding: const EdgeInsets.only(
bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: TextEditingController.fromValue(
TextEditingValue(
text: text2,
// selection: TextSelection.collapsed(offset: text.length),
),
),
),
),
SizedBox(
width: 200,
height: 50,
child: RaisedButton(
color: Colors.redAccent,
onPressed: () {
if (_formkey.currentState.validate()) {
RegistrationUser(_stopname.toString(),_platname.toString(),_platdesc.toString(),_direction.toString(),text,text2);
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"),

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

Future RegistrationUser(String _stopname,String _platname,String _platdesc,String _direction,text, text2) async {
var APIURL = "http://localhost:ip/add.hh?stop_name="+_stopname +"&plat_name="+_platname+"&plat_desc="+_platdesc+"&direction="+_direction+"&lat="+text+"&long="+text2+"";
print("123");
print(APIURL);
Map mappeddata = {
'stop_name': _stopname,
'plat_name': _platname,
'plat_desc': _platdesc,
'direction': _direction,
'lat': text,
'long': text2,

};
print("hi");
// print(text);
print("JSON DATA: ${mappeddata}");
http.Response reponse = await http.get(APIURL);
//getting response from php code, here
var data = jsonDecode(reponse.body);
print("DATA: ${data}");
}}
Reply all
Reply to author
Forward
0 new messages