Startup Namer Part - 4

What you’ll learn

What you’ll build

Tapping the list icon in the upper right of the app bar navigates to a new page (called a route) that lists only the favorited names.

image

Navigate to a new screen

In this step, you’ll add a new page (called a route in Flutter) that displays the favorites. You’ll learn how to navigate between the home route and the new route.

In Flutter, the Navigator manages a stack containing the app’s routes. Pushing a route onto the Navigator’s stack, updates the display to that route. Popping a route from the Navigator’s stack, returns the display to the previous route.

Next, you’ll add a list icon to the AppBar in the build method for RandomWordsState. When the user clicks the list icon, a new route that contains the saved favorites is pushed to the Navigator, displaying the icon.

class RandomWordsState extends State<RandomWords> {
  ...
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Startup Name Generator'),
        actions: <Widget>[      // Add 3 lines from here...
          IconButton(icon: Icon(Icons.list), onPressed: _pushSaved),
        ],                      // ... to here.
      ),
      body: _buildSuggestions(),
    );
  }
  ...
}

Tip: Some widget properties take a single widget (child), and other properties, such as action, take an array of widgets (children), as indicated by the square brackets ([]).

  void _pushSaved() {
  }

Next, you’ll build a route and push it to the Navigator’s stack. This action changes the screen to display the new route. The content for the new page is built in MaterialPageRoute’s builder property, in an anonymous function.

void _pushSaved() {
  Navigator.of(context).push(
  );
}

Next, you’ll add the MaterialPageRoute and its builder. For now, add the code that generates the ListTile rows. The divideTiles() method of ListTile adds horizontal spacing between each ListTile. The divided variable holds the final rows, converted to a list by the convenience function, toList().

void _pushSaved() {
  Navigator.of(context).push(
    MaterialPageRoute<void>(   // Add 20 lines from here...
      builder: (BuildContext context) {
        final Iterable<ListTile> tiles = _saved.map(
          (WordPair pair) {
            return ListTile(
              title: Text(
                pair.asPascalCase,
                style: _biggerFont,
              ),
            );
          },
        );
        final List<Widget> divided = ListTile
          .divideTiles(
            context: context,
            tiles: tiles,
          )
          .toList();
      },
    ),                       // ... to here.
  );
}

The builder property returns a Scaffold, containing the app bar for the new route, named “Saved Suggestions.” The body of the new route consists of a ListView containing the ListTiles rows; each row is separated by a divider.

void _pushSaved() {
  Navigator.of(context).push(
    MaterialPageRoute<void>(
      builder: (BuildContext context) {
        final Iterable<ListTile> tiles = _saved.map(
          (WordPair pair) {
            return ListTile(
              title: Text(
                pair.asPascalCase,
                style: _biggerFont,
              ),
            );
          },
        );
        final List<Widget> divided = ListTile
          .divideTiles(
            context: context,
            tiles: tiles,
          )
              .toList();

        return Scaffold(         // Add 6 lines from here...
          appBar: AppBar(
            title: Text('Saved Suggestions'),
          ),
          body: ListView(children: divided),
        );                       // ... to here.
      },
    ),
  );
}
iOS - Main route iOS - Saved suggestions route
image image

Change the UI using Themes

In this step, you’ll modify the app’s theme. The theme controls the look and feel of your app. You can either use the default theme, which is dependent on the physical device or emulator, or customize the theme to reflect your branding.

You can easily change an app’s theme by configuring the ThemeData class. This app currently uses the default theme, but you’ll change the app’s primary color to white.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Startup Name Generator',
      theme: ThemeData(          // Add the 3 lines from here... 
        primaryColor: Colors.white,
      ),                         // ... to here.
      home: RandomWords(),
    );
  }
}

As an exercise for the reader, use ThemeData to change other aspects of the UI. The Colors class in the Material library provides many color constants you can play with, and hot reload makes experimenting with the UI quick and easy.

Android iOS
image image

CHALLENGE

These days it’s hard to come by a good app that doesn’t feature any animations. The challenge is to create delightful experiences for the user by incorporating animations into your Startup Namer.

Your animation must include:

  1. Pulsating(growing and shrinking) effect when tapping the Heart Icon.

  2. Stagger List Animation (Sliding in one after the other) for the list of startup names.

You can add other animations if you wish to.

Update

If you find it difficult to create the same animations as mentioned here, you can use any **prepackaged flutter animations from https://pub.dev/

Suggestion:- Try animating the headings “Startup Name Generator” and the “Saved Suggestions” using any of the text animations from https://pub.dev/packages/animated_text_kit