page_selector_demo.dart 3.08 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 The Chromium 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
import '../../gallery/demo.dart';

Hans Muller's avatar
Hans Muller committed
9
class _PageSelector extends StatelessWidget {
10
  const _PageSelector({ this.icons });
11

12
  final List<Icon> icons;
13

14
  void _handleArrowButtonPress(BuildContext context, int delta) {
15
    final TabController controller = DefaultTabController.of(context);
Hans Muller's avatar
Hans Muller committed
16
    if (!controller.indexIsChanging)
17
      controller.animateTo((controller.index + delta).clamp(0, icons.length - 1));
18 19
  }

20
  @override
Hans Muller's avatar
Hans Muller committed
21 22 23
  Widget build(BuildContext context) {
    final TabController controller = DefaultTabController.of(context);
    final Color color = Theme.of(context).accentColor;
24
    return SafeArea(
25 26
      top: false,
      bottom: false,
27
      child: Column(
28
        children: <Widget>[
29
          Container(
30
            margin: const EdgeInsets.only(top: 16.0),
31
            child: Row(
32
              children: <Widget>[
33
                IconButton(
34 35 36 37
                  icon: const Icon(Icons.chevron_left),
                  color: color,
                  onPressed: () { _handleArrowButtonPress(context, -1); },
                  tooltip: 'Page back'
Hans Muller's avatar
Hans Muller committed
38
                ),
39 40
                TabPageSelector(controller: controller),
                IconButton(
41 42 43 44 45 46 47 48 49
                  icon: const Icon(Icons.chevron_right),
                  color: color,
                  onPressed: () { _handleArrowButtonPress(context, 1); },
                  tooltip: 'Page forward'
                )
              ],
              mainAxisAlignment: MainAxisAlignment.spaceBetween
            )
          ),
50 51 52
          Expanded(
            child: IconTheme(
              data: IconThemeData(
53 54 55
                size: 128.0,
                color: color,
              ),
56
              child: TabBarView(
57
                children: icons.map<Widget>((Icon icon) {
58
                  return Container(
59
                    padding: const EdgeInsets.all(12.0),
60 61
                    child: Card(
                      child: Center(
62 63
                        child: icon,
                      ),
64
                    ),
65 66 67
                  );
                }).toList()
              ),
68
            ),
Hans Muller's avatar
Hans Muller committed
69
          ),
70 71
        ],
      ),
Hans Muller's avatar
Hans Muller committed
72 73 74
    );
  }
}
75

Hans Muller's avatar
Hans Muller committed
76
class PageSelectorDemo extends StatelessWidget {
77
  static const String routeName = '/material/page-selector';
78 79 80 81 82 83 84
  static final List<Icon> icons = <Icon>[
    const Icon(Icons.event, semanticLabel: 'Event'),
    const Icon(Icons.home, semanticLabel: 'Home'),
    const Icon(Icons.android, semanticLabel: 'Android'),
    const Icon(Icons.alarm, semanticLabel: 'Alarm'),
    const Icon(Icons.face, semanticLabel: 'Face'),
    const Icon(Icons.language, semanticLabel: 'Language'),
Hans Muller's avatar
Hans Muller committed
85 86 87 88
  ];

  @override
  Widget build(BuildContext context) {
89
    return Scaffold(
90 91 92 93
      appBar: AppBar(
        title: const Text('Page selector'),
        actions: <Widget>[MaterialDemoDocumentationButton(routeName)],
      ),
94
      body: DefaultTabController(
Hans Muller's avatar
Hans Muller committed
95
        length: icons.length,
96
        child: _PageSelector(icons: icons),
Hans Muller's avatar
Hans Muller committed
97
      ),
98 99 100
    );
  }
}