メアド、パスワードを画面から入力
メアドとパスワードの入力欄と、ログインボタンを配置します。
入力時にstateを更新しています。
login.dart
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer <LoginStore>(
builder: (context, loginStore, _) {
return Scaffold(
body: Container(margin: EdgeInsets.symmetric(
vertical: 80.0, horizontal: 30.0),
child: Column(children: <Widget>[
TextFormField(
decoration: InputDecoration(
hintText: "メアド",
),
onChanged: (text) => loginStore.changeEmail(text),
),
TextFormField(
decoration: InputDecoration(
hintText: "パスワード",
),
onChanged: (text) =>
loginStore.changePassword(text),
),
RaisedButton(
child: Text("Login")
,
onPressed: () {
loginStore.tapLoginButton();
}),
],)
));
});
}
}
認証処理
signInWithEmailAndPassword で認証を行います。
login.dart
class LoginStore with ChangeNotifier {
var email = "";
var password = "";
void changeEmail(String text) {
email = text;
}
void changePassword(String text) {
password = text;
}
void tapLoginButton() async {
AuthResult result = await _signIn(email, password);
}
Future<AuthResult> _signIn(String email, String password) async {
try {
final AuthResult result = await FirebaseAuth.instance
.signInWithEmailAndPassword(
email: email, password: password);
return result;
} catch (e) {
return null;
}
}
}