Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Playground',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
body: Align(
alignment: Alignment.bottomCenter,
child: ResponsiveInput(),
),
);
}
}
class ResponsiveInput extends StatelessWidget {
ResponsiveInput({Key key}) : super(key: key);
TextEditingController emailTextEditingController = TextEditingController();
TextEditingController passwordTextEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
padding: EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: emailTextEditingController,
cursorColor: Colors.black,
obscureText: false,
onSaved: (input) {},
),
TextFormField(
controller: passwordTextEditingController,
cursorColor: Colors.black,
obscureText: false,
onSaved: (input) {},
),
TextButton(onPressed: (){
LoginRequestModel login = LoginRequestModel(
email: emailTextEditingController.text,
password: passwordTextEditingController.text,
);
Auth().login(login);
}, child: Text("Login"))
],
),
);
}
}
class LoginRequestModel {
String email;
String password;
LoginRequestModel({ this.email, this.password} );
factory LoginRequestModel.fromJson(Map<String, dynamic> json) {
return LoginRequestModel(
email: json['email'],
password: json['password']
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['email'] = this.email;
data['password'] = this.password;
return data;
}
}
class Auth {
static String baseUrl = "MY_API";
var client = http.Client();
void login(LoginRequestModel model) async {
print(model.toJson());
await client.post(Uri.parse(baseUrl), body: jsonEncode(model.toJson()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment