navigation_bar.1.dart 2.89 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7 8
/// Flutter code sample for [NavigationBar].

9
void main() => runApp(const NavigationBarApp());
10

11 12
class NavigationBarApp extends StatelessWidget {
  const NavigationBarApp({super.key});
13 14 15

  @override
  Widget build(BuildContext context) {
16
    return const MaterialApp(home: NavigationExample());
17 18 19
  }
}

20 21
class NavigationExample extends StatefulWidget {
  const NavigationExample({super.key});
22

23 24 25
  @override
  State<NavigationExample> createState() => _NavigationExampleState();
}
26

27 28 29
class _NavigationExampleState extends State<NavigationExample> {
  int currentPageIndex = 0;
  NavigationDestinationLabelBehavior labelBehavior = NavigationDestinationLabelBehavior.alwaysShow;
30 31 32 33

  @override
  Widget build(BuildContext context) {
    return Scaffold(
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      bottomNavigationBar: NavigationBar(
        labelBehavior: labelBehavior,
        selectedIndex: currentPageIndex,
        onDestinationSelected: (int index) {
          setState(() {
            currentPageIndex = index;
          });
        },
        destinations: const <Widget>[
          NavigationDestination(
            icon: Icon(Icons.explore),
            label: 'Explore',
          ),
          NavigationDestination(
            icon: Icon(Icons.commute),
            label: 'Commute',
          ),
          NavigationDestination(
            selectedIcon: Icon(Icons.bookmark),
            icon: Icon(Icons.bookmark_border),
            label: 'Saved',
          ),
        ],
57 58 59
      ),
      body: Center(
        child: Column(
60
          mainAxisAlignment: MainAxisAlignment.center,
61
          children: <Widget>[
62 63 64 65 66 67
            Text('Label behavior: ${labelBehavior.name}'),
            const SizedBox(height: 10),
            OverflowBar(
              spacing: 10.0,
              children: <Widget>[
                ElevatedButton(
68
                  onPressed: () {
69 70 71
                    setState(() {
                      labelBehavior = NavigationDestinationLabelBehavior.alwaysShow;
                    });
72
                  },
73
                  child: const Text('alwaysShow'),
74
                ),
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      labelBehavior = NavigationDestinationLabelBehavior.onlyShowSelected;
                    });
                  },
                  child: const Text('onlyShowSelected'),
                ),
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      labelBehavior = NavigationDestinationLabelBehavior.alwaysHide;
                    });
                  },
                  child: const Text('alwaysHide'),
                ),
              ],
92
            ),
93
          ],
94 95 96 97 98
        ),
      ),
    );
  }
}