Very simple login form, works on iOS and web, but not on Android. Is there something fundamental, or is there some bug related to this. Basically, clicking on the "submit" button does not make make the login call.
The AppState is a singelton class in case you want to know.
Basically it is identical to the form defined in the Flutter documentation: https://flutter.dev/docs/cookbook/forms/validation
import 'package:flutter/material.dart';
import 'package:flutter_poc/app_state.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
class AuthPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(child: MyCustomForm()),
),
);
}
}
class MyCustomForm extends StatefulWidget {
@override
MyCustomFormState createState() {
return MyCustomFormState();
}
}
class MyCustomFormState extends State<MyCustomForm> {
final _formKey = GlobalKey<FormState>();
String _username;
String _password;
Future<String> getAuthToken(username, password) async {
final postData = {'username': username, 'password': password};
final loginUrl = 'https://xxx/login';
final response = await http.post(loginUrl, body: postData);
if (response.statusCode == 200) {
final data = convert.jsonDecode(response.body);
return data['token'];
}
return null;
}
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
decoration: InputDecoration(hintText: 'Email'),
validator: (value) {
if (value.isEmpty) {
return 'Enter email';
}
return null;
},
onSaved: (val) => {_username = val},
),
TextFormField(
decoration: InputDecoration(hintText: 'Password'),
validator: (value) {
if (value.isEmpty) {
return 'Enter password';
}
return null;
},
obscureText: true,
onSaved: (val) => {_password = val},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () async {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
final token = await getAuthToken(_username, _password);
if (token == null) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('failed to login')));
} else {
AppState().setToken(token);
}
}
},
child: Text('Submit'),
),
),
],
),
);
}
}