mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
move experimental/form_app to root of project (#623)
This allows us to reference this sample from other places. See https://github.com/flutter/flutter/pull/70321 for more context.
This commit is contained in:
124
form_app/lib/src/sign_in_http.dart
Normal file
124
form_app/lib/src/sign_in_http.dart
Normal file
@@ -0,0 +1,124 @@
|
||||
// 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(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user