Step 4: Add Expansion Panels and Tabs 0.11


In this final step, you’ll use expansion panels and tabs to hide information until the user needs it. You’ll use these components:

  • <material-expansionpanel> and <material-expansionpanel-set>
  • <material-tab> and <material-tab-panel>

Run the live example (view source) of the 4-final version of the app.

Use material-expansionpanel and material-expansionpanel-set

Expansion panels are especially good for settings, so use them in the custom <settings-component>, implemented in lib/src/settings/settings_component.* files.

  1. Edit the Dart file (lib/src/settings/settings_component.dart), adding the expansion panel directives MaterialExpansionPanel and MaterialExpansionPanelSet:

    {3-usebuttons → 4-final}/lib/src/settings/settings_component.dart
    @@ -15,6 +15,8 @@
    15
    15
    templateUrl: 'settings_component.html',
    16
    16
    directives: [
    17
    17
    MaterialCheckboxComponent,
    18
    + MaterialExpansionPanel,
    19
    + MaterialExpansionPanelSet,
    18
    20
    MaterialRadioComponent,
    19
    21
    MaterialRadioGroupComponent,
    20
    22
    NgFor
  2. Edit the template file (lib/src/settings/settings_component.html) to change the enclosing <div> element (the first and last lines of the file) to be a <material-expansionpanel-set>.

  3. Convert the Wallet div into a <material-expansionpanel> (MaterialExpansionPanel):

    1. Change the <div> element to <material-expansionpanel>.
    2. Move the title (h2 content) and summary content (p content) into the <material-expansionpanel> name and secondaryText attributes, respectively.
    3. Remove the buttons from the bottom of the panel, putting their event handling code into (save) and (cancel) bindings. Your code changes to the beginning of this file should look like this:
    {3-usebuttons → 4-final}/lib/src/settings/settings_component.html
    @@ -1,7 +1,9 @@
    1
    - <div>
    2
    - <div>
    3
    - <h2>Wallet</h2>
    4
    - <p>Initial: ${{ settings.initialCash }}. Daily disposable income: ${{ settings.dailyDisposable }}.</p>
    1
    + <material-expansionpanel-set>
    2
    + <material-expansionpanel
    3
    + name="Wallet"
    4
    + secondaryText="Initial: ${{ settings.initialCash }}. Daily disposable income: ${{ settings.dailyDisposable }}."
    5
    + (save)="settingsUpdated()"
    6
    + (cancel)="resetWallet()">
    5
    7
    <div>
    6
    8
    <h3>Initial cash</h3>
    7
    9
    <material-radio-group>
    @@ -21,7 +23,3 @@
    21
    23
    </material-radio>
    22
    24
    </material-radio-group>
    23
    25
    </div>
    24
    - <button (click)="settingsUpdated()">Save</button>
    25
    - <button (click)="resetWallet()">Cancel</button>
    26
    - </div>
    27
    - <div class="betting-panel">
  4. Test the app. The Wallet settings should look like this at first:

    screenshot

    When you expand the Wallet settings, they should look like this:

    screenshot

    When you change settings and click the SAVE button at bottom right, the new values should appear in the UI:

    screenshot

  5. Once the app runs correctly, convert the two remaining major divs (Betting and Other) into material expansion panels.

That bit of work saved a lot of UI space:

screenshot>

Use material-tab and material-tab-panel

You can save more by moving auxiliary text into separate tabs. This affects the main UI, implemented in lib/lottery_simulator.* files.

  1. Make the following changes to lib/lottery_simulator.dart:

    {3-usebuttons → 4-final}/lib/lottery_simulator.dart
    @@ -28,6 +28,8 @@
    28
    28
    MaterialFabComponent,
    29
    29
    MaterialIconComponent,
    30
    30
    MaterialProgressComponent,
    31
    + MaterialTabComponent,
    32
    + MaterialTabPanelComponent,
    31
    33
    MaterialToggleComponent,
    32
    34
    ScoresComponent,
    33
    35
    SettingsComponent,
  2. Edit lib/lottery_simulator.html:

    1. After the end of the first <div>, add a <material-tab-panel> tag.

    2. Put the closing tag of the <material-tab-panel> at the end of the file.

    3. Just after the opening <material-tab-panel> tag, add a <material-tab> tag with the label set to “Simulation”.

    4. Close the <material-tab-panel> near the bottom of the file, just before the <div> containing the Help heading.

      Your changes, so far, should look like this:

      <h1>Lottery Simulator</h1>
      
      <div class="help">
        ...
      </div>
      
      <material-tab-panel>
        <material-tab label="Simulation">
          ...
        </material-tab>
      <div>
        <h2>Help`</h2>`
        ...
      </div>
      </material-tab-panel>

      If you run the app now, the top of the UI should look like this:

      screenshot: a "Simulation" tab is visible above the "Playing Powerball heading">

    5. Change the next two <div>-<h2> combinations into <material-tabs>, with the labels “Help” and “About”.

The end of the file should look like this:

{3-usebuttons → 4-final}/lib/lottery_simulator.html
@@ -85 +78,7 @@
78
+ </material-tab>
79
+ <material-tab label="Help">
80
+ <help-component content="help"></help-component>
81
+ </material-tab>
82
+ <material-tab label="About">
83
+ <help-component content="about"></help-component>
84
+ </material-tab>
85
+ </material-tab-panel>

Your app should now look exactly like the one you saw in the first page of this codelab.

Congratulations! You’ve converted a functional but UI-challenged app into a good looking, well-behaved app that uses the next generation of AngularDart Components.

Problems?

Check your code against the solution in the 4-final directory.