1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-06 03:31:03 +00:00
Files
samples/experimental/web_dashboard/lib/src/pages/sign_in.dart
John Ryan 8a9bcfa113 [web_dashboard] add logout (#447)
* logout wip

* Use AnimatedSwitcher, change lingo from "login" to signIn"

* add automatic sign-in

* fix flashing sign in button

* sign out of FirebaseAuth and GoogleSignIn

* formatting

* change isSignedIn() to getter

* Add error handling for sign in

* improve error handling at login screen
2020-06-01 14:39:06 -07:00

105 lines
2.5 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 'package:flutter/material.dart';
import '../auth/auth.dart';
class SignInPage extends StatelessWidget {
final Auth auth;
final ValueChanged<User> onSuccess;
SignInPage({
@required this.auth,
@required this.onSuccess,
});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SignInButton(auth: auth, onSuccess: onSuccess),
),
);
}
}
class SignInButton extends StatefulWidget {
final Auth auth;
final ValueChanged<User> onSuccess;
SignInButton({
@required this.auth,
@required this.onSuccess,
});
@override
_SignInButtonState createState() => _SignInButtonState();
}
class _SignInButtonState extends State<SignInButton> {
Future<bool> _checkSignInFuture;
@override
void initState() {
super.initState();
_checkSignInFuture = _checkIfSignedIn();
}
// Check if the user is signed in. If the user is already signed in (for
// example, if they signed in and refreshed the page), invoke the `onSuccess`
// callback right away.
Future<bool> _checkIfSignedIn() async {
var alreadySignedIn = await widget.auth.isSignedIn;
if (alreadySignedIn) {
var user = await widget.auth.signIn();
widget.onSuccess(user);
}
return alreadySignedIn;
}
Future<void> _signIn() async {
try {
var user = await widget.auth.signIn();
widget.onSuccess(user);
} on SignInException {
_showError();
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: _checkSignInFuture,
builder: (context, snapshot) {
// If signed in, or the future is incomplete, show a circular
// progress indicator.
var alreadySignedIn = snapshot.data;
if (snapshot.connectionState != ConnectionState.done ||
alreadySignedIn == true) {
return CircularProgressIndicator();
}
// If sign in failed, show toast and the login button
if (snapshot.hasError) {
_showError();
}
return RaisedButton(
child: Text('Sign In with Google'),
onPressed: () => _signIn(),
);
},
);
}
void _showError() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('Unable to sign in.'),
),
);
}
}