1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-10 23:08:59 +00:00
Files
samples/experimental/form_app/lib/src/sign_in_http.dart
John Ryan af5be70f34 add form_app (#463)
* add form_app

* revert unrelated changes

* use links in README

* remove unnecessary type annotation

* Remove GlobalKey for sign_in_http sample

* reword comment

* run package:copywriter

* use relative import

* fix quotes

* Add punctuation

* make sure setState() is used in form_widgets sample

* avoid first person pronoun

* store date state in parent widget in form_widgets.dart
2020-06-12 12:38:46 -07:00

125 lines
3.4 KiB
Dart

// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
part 'sign_in_http.g.dart';
@JsonSerializable()
class FormData {
String email;
String password;
FormData({
this.email,
this.password,
});
factory FormData.fromJson(Map<String, dynamic> json) =>
_$FormDataFromJson(json);
Map<String, dynamic> toJson() => _$FormDataToJson(this);
}
class SignInHttpDemo extends StatefulWidget {
final http.Client httpClient;
SignInHttpDemo({
this.httpClient,
});
@override
_SignInHttpDemoState createState() => _SignInHttpDemoState();
}
class _SignInHttpDemoState extends State<SignInHttpDemo> {
FormData formData = FormData();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sign in Form'),
),
body: Form(
child: Scrollbar(
child: SingleChildScrollView(
padding: EdgeInsets.all(16),
child: Column(
children: [
...[
TextFormField(
decoration: InputDecoration(
filled: true,
hintText: 'Your email address',
labelText: 'Email',
),
onChanged: (value) {
formData.email = value;
},
),
TextFormField(
decoration: InputDecoration(
filled: true,
labelText: 'Password',
),
obscureText: true,
onChanged: (value) {
formData.password = value;
},
),
FlatButton(
child: Text('Sign in'),
onPressed: () async {
// Use a JSON encoded string to send
var result = await widget.httpClient.post(
'https://example.com/signin',
body: json.encode(formData.toJson()),
headers: {'content-type': 'application/json'});
if (result.statusCode == 200) {
_showDialog('Succesfully signed in.');
} else if (result.statusCode == 401) {
_showDialog('Unable to sign in.');
} else {
_showDialog('Something went wrong. Please try again.');
}
},
),
].expand(
(widget) => [
widget,
SizedBox(
height: 24,
)
],
)
],
),
),
),
),
);
}
void _showDialog(String message) {
showDialog(
context: context,
child: AlertDialog(
title: Text(message),
actions: [
FlatButton(
child: Text('OK'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
}
}