Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart';
- class LoginForm extends StatefulWidget {
- @override
- _LoginFormState createState() => _LoginFormState();
- }
- class _LoginFormState extends State<LoginForm> {
- GlobalKey<FormState> _formKey = GlobalKey();
- String username = "";
- String password = "";
- String message ="";
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Center(child: Text("Login Form")),
- ),
- body: Form(
- key: _formKey,
- child: Column(
- children: <Widget>[
- SizedBox(
- height: 20,
- ),
- Text(message),
- TextFormField(
- decoration: InputDecoration(
- hintText: "Username",
- icon: Icon(Icons.person),
- ),
- validator: (value){
- if(value.isEmpty) return "Username cannot be empty";
- return null;
- },
- onSaved: (value){
- username=value;
- },
- ),
- TextFormField(
- decoration: InputDecoration(
- hintText: "Password",
- icon: Icon(Icons.vpn_key),
- ),
- validator: (value){
- if(value.isEmpty) return "Password cannot be empty";
- return null;
- },
- onSaved: (value){
- password=value;
- },
- ),
- RaisedButton(
- onPressed: this._onLoginClicked,
- child: Text("Login"),
- )
- ],
- ),
- ),
- );
- }
- _onLoginClicked() async{
- if(!_formKey.currentState.validate())return;
- _formKey.currentState.save();
- Map<String,dynamic> body = {
- 'username':username,
- 'password':password
- };
- try{
- Response response=await post('http://flutter.sochware.com/api/login',body:body);
- print("COde : ${response.statusCode}");
- print(response.body);
- var jsonData=json.decode(response.body);
- setState((){
- message=jsonData['message'];
- });
- }catch(error){
- print(error);
- if(error is SocketException){
- setState(() {
- message="Internet not available";
- });
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement