A month of Flutter: navigate to user registration

With users now being able to sign in with Google, I want to have them confirm their name and agree to a terms of service. To lay the groundwork I'm going to add a placeholder RegisterPage and navigation to it.

This will be my second page, of what I expect to be many, so I went ahead and created a pages directory. The existing MyHomePage class was renamed to HomePage and relocated to the pages directory. That's a refactor and ideally should be in its own pull request, but it's of minimal impact so I'm lumping in with today's other changes.

The new RegisterPage is simple and currently just displays some text. On the first implementation I did find that the AppBar title was no not centered.

register page with off center title

This is because I was placing the text in a Center widget. Center was centering the text in the space available to it but the back navigation icon was not being accounted for.

AppBar(
  title: Center(
    child: Text('Register'),
  ),
  elevation: 0.0,
)

A quick switch to using centerTitle fixes it.

AppBar(
  title: Text('Register'),
  centerTitle: true,
  elevation: 0.0,
)

register page

But how do users get to this new registration page? It has to happen after a user is authenticated (how else will I know if they are registered?) but before they perform any other authenticated actions. That only leaves _handleSignIn in SignInFab.

void _handleSignIn(BuildContext context) {
  auth.signInWithGoogle().then((FirebaseUser user) {
    if (_existingUser()) {
      _showSnackBar(context, 'Welcome ${user.displayName}');
    } else {
      _navigateToRegistration(context);
    }
  });
}

For now I've set _existingUser to return a hardcoded boolean. If they are an existing user they should just see the SnackBar, otherwise send them to the RegisterPage.

void _navigateToRegistration(BuildContext context) {
  Navigator.pushNamed(context, RegisterPage.routeName);
}

_navigateToRegistration uses the Navigator widget to push a new route onto the stack by name. In this case I'm defining the routeName in RegisterPage itself so if I want to change it later, I only have to change it in one place.

In MyApp I will now register the route with MaterialApp, also using the routeName from RegisterPage.

MaterialApp(
  // ...
  home: const HomePage(title: 'Birb'),
  routes: <String, WidgetBuilder>{
    RegisterPage.routeName: (BuildContext context) => const RegisterPage(),
  },
);

Code changes

Posts in this series

  • A month of Flutter
  • A month of Flutter: create the app
  • A month of Flutter: configuring continuous integration
  • A month of Flutter: continuous linting
  • A month of Flutter: upgrading to 1.0
  • A month of Flutter: initial theme
  • A month of Flutter: no content widget
  • A month of Flutter: a list of posts
  • A month of Flutter: extract post item widget
  • A month of Flutter: post model and mock data
  • A month of Flutter: rendering a ListView with StreamBuilder
  • A month of Flutter: Stream transforms and failing tests
  • A month of Flutter: real faker data
  • A month of Flutter: rendering network images
  • A month of Flutter: FABulous authentication
  • A month of Flutter: configure Firebase Auth for Sign in with Google on Android
  • A month of Flutter: configure Firebase Auth for Sign in with Google on iOS
  • A month of Flutter: Sign in with Google
  • A month of Flutter: mocking Firebase Auth in tests
  • A month of Flutter: delicious welcome snackbar
  • A month of Flutter: navigate to user registration
  • A month of Flutter: user registration form
  • A month of Flutter: testing forms
  • A month of Flutter: setting up Firebase Firestore
  • A month of Flutter: awesome adaptive icons
  • A month of Flutter: set up Firestore rules tests
  • A month of Flutter: Firestore create user rules and tests
  • A month of Flutter: WIP save users to Firestore
  • A month of Flutter: user registration refactor with reactive scoped model
  • A month of Flutter: the real hero animation
  • A month of Flutter: a look back

  • Category: Development