【Flutter/FirebaseAuth】ログイン機能の実装準備|スクリーン作成と画面遷移

FirebaseAuthを使用してログイン機能を実装していく下準備として、ログイン画面や新規登録画面などのスクリーン、画面遷移ボタンを作成してみたので紹介します。

テスト開発用として良かったら使ってみてください。

\ 世界最大級のオンライン学習サービス /

目次

スクリーン画像&コピペ用コード

  • lib
    • main.dart
    • screens
      • welcome_screen.dart
      • home_screen.dart
      • register_screen.dart
      • email_login_screen.dart
      • forgot_password_screen.dart

main.dart

import 'package:flutter/material.dart';
import 'package:myapp/screens/welcome_screen.dart';
import 'package:myapp/screens/home_screen.dart';
import 'package:myapp/screens/register_screen.dart';
import 'package:myapp/screens/email_login_screen.dart';
import 'package:myapp/screens/forgot_password_screen.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: WelcomeScreen.id,
      routes: {
        WelcomeScreen.id: (context) => const WelcomeScreen(),
        EmailLoginScreen.id: (context) => const EmailLoginScreen(),
        HomeScreen.id: (context) => const HomeScreen(),
        RegisterScreen.id: (context) => const RegisterScreen(),
        ForgotPasswordScreen.id: (context) => const ForgotPasswordScreen(),
      },
    );
  }
}

ログイン画面(WelcomeScreen)

import 'package:flutter_signin_button/flutter_signin_button.dart';
import 'package:flutter/material.dart';
import 'package:myapp/screens/home_screen.dart';
import 'package:myapp/screens/register_screen.dart';
import 'package:myapp/screens/email_login_screen.dart';
import 'package:myapp/screens/forgot_password_screen.dart';

class WelcomeScreen extends StatefulWidget {
  static final id = 'WelcomeScreen';
  const WelcomeScreen({Key? key}) : super(key: key);

  @override
  State<WelcomeScreen> createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Welcome'),
        automaticallyImplyLeading: false,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SignInButton(
              Buttons.Email,
              text: 'Emailでログイン',
              onPressed: () =>
                  Navigator.pushNamed(context, EmailLoginScreen.id),
            ),
            SignInButton(
              Buttons.Google,
              text: 'Googleでログイン',
              onPressed: () => Navigator.pushNamed(context, HomeScreen.id),
            ),
            SignInButtonBuilder(
              icon: Icons.account_circle,
              text: 'ゲストログイン',
              backgroundColor: Colors.blue,
              onPressed: () => Navigator.pushNamed(context, HomeScreen.id),
            ),
            SizedBox(height: 30),
            TextButton(
              child: Text('新規登録はこちら'),
              onPressed: () => Navigator.pushNamed(context, RegisterScreen.id),
            ),
            TextButton(
              child: Text('パスワードを忘れた'),
              onPressed: () =>
                  Navigator.pushNamed(context, ForgotPasswordScreen.id),
            )
          ],
        ),
      ),
    );
  }
}

メインコンテンツ(HomeScreen)

import 'package:flutter_signin_button/flutter_signin_button.dart';
import 'package:flutter/material.dart';
import 'package:myapp/screens/welcome_screen.dart';

class HomeScreen extends StatefulWidget {
  static final id = 'HomeScreen';
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:
          AppBar(title: const Text('Home'), automaticallyImplyLeading: false),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('メインコンテンツ'),
            SizedBox(height: 20),
            SignInButtonBuilder(
              icon: Icons.logout,
              text: 'ログアウト',
              backgroundColor: Colors.grey,
              onPressed: () => Navigator.pushNamed(context, WelcomeScreen.id),
            ),
          ],
        ),
      ),
    );
  }
}

新規登録画面(RegisterScreen)

import 'package:flutter/material.dart';
import 'package:myapp/screens/home_screen.dart';

class RegisterScreen extends StatefulWidget {
  static final id = 'RegisterScreen';
  const RegisterScreen({Key? key}) : super(key: key);

  @override
  State<RegisterScreen> createState() => _RegisterScreenState();
}

class _RegisterScreenState extends State<RegisterScreen> {
  String? email;
  String? password;

  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Register')),
      body: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          children: [
            TextField(
              controller: _emailController,
              onChanged: (email) {
                this.email = email;
              },
              decoration: InputDecoration(hintText: 'Email'),
            ),
            TextField(
              controller: _passwordController,
              onChanged: (password) {
                this.password = password;
              },
              obscureText: true,
              decoration: InputDecoration(hintText: 'Password'),
            ),
            ElevatedButton(
              child: Text('新規登録'),
              onPressed: () => Navigator.pushNamed(context, HomeScreen.id),
            )
          ],
        ),
      ),
    );
  }
}

メール・ログイン画面(EmailLoginScreen)

import 'package:flutter/material.dart';
import 'package:myapp/screens/home_screen.dart';

class EmailLoginScreen extends StatefulWidget {
  static final id = 'EmailLoginScreen';
  const EmailLoginScreen({Key? key}) : super(key: key);

  @override
  State<EmailLoginScreen> createState() => _EmailLoginScreenState();
}

class _EmailLoginScreenState extends State<EmailLoginScreen> {
  String? email;
  String? password;

  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Sign In')),
      body: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          children: [
            TextField(
              controller: _emailController,
              onChanged: (email) {
                this.email = email;
              },
              decoration: InputDecoration(hintText: 'Email'),
            ),
            TextField(
              controller: _passwordController,
              onChanged: (password) {
                this.password = password;
              },
              obscureText: true,
              decoration: InputDecoration(hintText: 'Password'),
            ),
            ElevatedButton(
              child: Text('ログイン'),
              onPressed: () => Navigator.pushNamed(context, HomeScreen.id),
            )
          ],
        ),
      ),
    );
  }
}

パワスワードリセット画面(ForgotPasswordScreen)

import 'package:flutter_signin_button/flutter_signin_button.dart';
import 'package:flutter/material.dart';
import 'package:myapp/screens/home_screen.dart';

class ForgotPasswordScreen extends StatefulWidget {
  static final id = 'ForgotPasswordScreen';
  const ForgotPasswordScreen({Key? key}) : super(key: key);

  @override
  State<ForgotPasswordScreen> createState() => _ForgotPasswordScreenState();
}

class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
  String? email;
  final _emailController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Forgot Password')),
      body: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          children: [
            TextField(
              controller: _emailController,
              onChanged: (email) {
                this.email = email;
              },
              decoration: InputDecoration(hintText: 'Email'),
            ),
            ElevatedButton(
              child: Text('パスワードをリセット'),
              onPressed: () => Navigator.pushNamed(context, HomeScreen.id),
            )
          ],
        ),
      ),
    );
  }
}

一緒に読みたい

  • URLをコピーしました!
目次