I feel as if this should be something very easy but I cannot figure it out, I have everything working I just need to make the 'email' and 'password' fields change to whatever the user enters. Below is my controller where the data gets sent via HTTP
postData() async {
try {
var response = await http
.post(Uri.parse("http://exampleIP/untitled/authentication/login.php"), body: {
"username": 'email', // needs to be what user inputs//
"password": 'password' // needs to be what user inputs//
I understand all the code is not there but the code works I just need to know how to get the input from the form
Here is form code
import 'package:flutter/material.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:flutx/flutx.dart';
import 'package:harralander/images.dart';
import 'package:harralander/theme/app_theme.dart';
import '../controllers/login_controller.dart';
class LogInScreen extends StatefulWidget {
const LogInScreen({Key? key}) : super(key: key);
@override
_LogInScreenState createState() => _LogInScreenState();
}
class _LogInScreenState extends State<LogInScreen> {
late ThemeData theme;
late CustomTheme customTheme;
late LogInController controller;
late OutlineInputBorder outlineInputBorder;
@override
void initState() {
super.initState();
theme = AppTheme.theme;
customTheme = AppTheme.customTheme;
controller = FxControllerStore.putOrFind(LogInController());
outlineInputBorder = OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(
color: Colors.transparent,
),
);
}
@override
Widget build(BuildContext context) {
return FxBuilder<LogInController>(
controller: controller,
builder: (controller) {
return _buildBody();
});
}
Widget _buildBody() {
return Scaffold(
body: Padding(
padding: FxSpacing.x(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FxText.h3(
'Log In',
fontWeight: 700,
),
FxSpacing.height(20),
Form(
key: controller.formKey,
child: Column(
children: [
TextFormField(
style: FxTextStyle.b2(),
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
filled: true,
isDense: true,
fillColor: customTheme.card,
prefixIcon: Icon(
FeatherIcons.mail,
color: theme.colorScheme.onBackground,
),
hintText: "Email Address",
enabledBorder: outlineInputBorder,
focusedBorder: outlineInputBorder,
border: outlineInputBorder,
contentPadding: FxSpacing.all(16),
hintStyle: FxTextStyle.b2(),
isCollapsed: true),
maxLines: 1,
controller: controller.emailTE,
validator: controller.validateEmail,
cursorColor: theme.colorScheme.onBackground,
),
FxSpacing.height(20),
TextFormField(
style: FxTextStyle.b2(),
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
filled: true,
isDense: true,
fillColor: customTheme.card,
prefixIcon: Icon(
FeatherIcons.lock,
color: theme.colorScheme.onBackground,
),
hintText: "Password",
enabledBorder: outlineInputBorder,
focusedBorder: outlineInputBorder,
border: outlineInputBorder,
contentPadding: FxSpacing.all(16),
hintStyle: FxTextStyle.b2(),
isCollapsed: true),
maxLines: 1,
controller: controller.passwordTE,
validator: controller.validatePassword,
cursorColor: theme.colorScheme.onBackground,
),
],
),
),
FxSpacing.height(20),
Align(
alignment: Alignment.centerRight,
child: FxButton.text(
onPressed: () {
controller.goToForgotPasswordScreen();
},
padding: FxSpacing.zero,
splashColor: customTheme.fitnessPrimary.withAlpha(40),
child: FxText.b3(
'Forgot password ?',
color: customTheme.fitnessPrimary,
)),
),
FxSpacing.height(20),
Row(
children: [
FxButton(
padding: FxSpacing.xy(16, 12),
onPressed: () {
controller.postData();
},
backgroundColor: customTheme.card,
splashColor: theme.colorScheme.onBackground.withAlpha(40),
elevation: 0,
borderRadiusAll: 4,
child: Row(
children: [
Image(
image: AssetImage(Images.google),
height: 17,
width: 17,
),
FxSpacing.width(20),
FxText.l2(
'Login with Google',
fontWeight: 600,
color: theme.colorScheme.onBackground,
),
],
)),
FxSpacing.width(20),
Expanded(
child: FxButton(
padding: FxSpacing.y(12),
onPressed: () {
controller.postData();
},
backgroundColor: customTheme.fitnessPrimary,
elevation: 0,
borderRadiusAll: 4,
child: FxText.b2(
'Log In',
color: customTheme.fitnessOnPrimary,
fontWeight: 600,
),
),
),
],
),
FxSpacing.height(20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FxText.b3(
'New to Harralander? ',
),
FxButton.text(
onPressed: () {
controller.goToRegisterScreen();
},
padding: FxSpacing.zero,
splashColor: customTheme.fitnessPrimary.withAlpha(40),
child: FxText.b3(
'Register',
color: customTheme.fitnessPrimary,
)),
],
),
],
),
),
);
}
}
Thank you
CodePudding user response:
Try this on your LogInController declare a TextEditingController
xample:
class LogInController extends GetxController{
// xample as your textediting as this
final emailTE = TextEditingController()
, passwordTE = TextEditingControler();
callToLoging() async{
//validate first as you use formKey
if(formKey.currentState!.validate()){
//call the loginApiService
await LoginApiService.postData(
email: emailTE.value!.text // or email!.text,
password : passwordTE.value!.text // or password!.text,
);
}
}
}
class LoginAPIService{
//Use Future or static it depends on you
static postData({String? email , String? password}) async {
try {
var response = await http
.post(Uri.parse("http://exampleIP/untitled/authentication/login.php"), body: {
"username": email ?? "", // needs to be what user inputs//
"password": password ?? ""});
} catch(e){
}
}
}
mention me if something happens if it works or not.
CodePudding user response:
you have to TextEditingController to get user input, and store those in varibale or access through using .text method. Eg:
TextEditingController _passwordController = new TextEditingController();
TextEditingController _emailController = new TextEditingController();
String? uName;
String? pwd;
//inside from:
//we can get data using
uName = _emailController.text;
pwd = _passwordController.text;
//or we can pass this directly as a parameter like this:
_passwordController.text
import 'package:flutter/material.dart'; import 'package:flutterloginform/validation.dart';
class LoginFormWithAutoValidation extends StatefulWidget {
@override
_LoginFormWithAutoValidationState createState() => _LoginFormWithAutoValidationState();
}
class _LoginFormWithAutoValidationState extends State<LoginFormWithAutoValidation> {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
TextEditingController _passwordController = new TextEditingController();
TextEditingController _emailController = new TextEditingController();
final _formKey = new GlobalKey<FormState>();
String _email;
String _password;
bool _autoValidate = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green[400],
body: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
color: Color(0xffF3F3F3),
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Form(
key: _formKey,
autovalidate: _autoValidate,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"Your Logo Here",
style: TextStyle(
color: Colors.black,
fontSize: 25,
),
)),
SizedBox(height: 25.0),
// TextFormField for email address
TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
controller: _emailController,
validator: validateEmail,
onSaved: (value) => _email = value,
style: style,
decoration: InputDecoration(
contentPadding:
EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Email",
border: OutlineInputBorder(
borderRadius:BorderRadius.circular(20.0))),
),
SizedBox(height: 25.0),
// TextFormField for email address
TextFormField(
autofocus: false,
controller: _passwordController,
validator: validatePassword,
onSaved: (value) => _password = value,
style: style,
decoration: InputDecoration(
contentPadding:
EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0))),
),
SizedBox(height: 25.0),
Divider(color: Colors.black), // divider
SizedBox(height: 20.0),
Material( // login button
elevation: 5.0,
borderRadius: BorderRadius.circular(20.0),
color: Colors.green,
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {
if (_formKey.currentState.validate()) {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return LoginFormWithAutoValidation();
}));
} else {
setState(() {
// validation error
_autoValidate = true;
});
}
},
child: Text("Login",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20)),
),
),
SizedBox(height: 10.0),
],
),
),
),
),
),
),
),
);
}