tabs_demo.dart 2.33 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 9 10 11 12 13 14 15 16 17 18 19 20 21
class _Page {
  _Page({ this.label });

  final GlobalKey<ScrollableState<Scrollable>> key = new GlobalKey<ScrollableState<Scrollable>>();
  final String label;
}

final List<_Page> _pages = <_Page>[
  new _Page(label: 'ONE'),
  new _Page(label: 'TWO'),
  new _Page(label: 'FREE'),
  new _Page(label: 'FOUR')
];

class TabsDemo extends StatefulWidget {
22 23
  static const String routeName = '/tabs';

24 25 26 27 28 29 30 31 32 33 34 35 36
  @override
  TabsDemoState createState() => new TabsDemoState();
}

class TabsDemoState extends State<TabsDemo> {
  _Page _selectedPage;
  double _scrollOffset = 0.0;

  @override
  void initState() {
    super.initState();
    _selectedPage = _pages[0];
  }
37

38
  @override
39
  Widget build(BuildContext context) {
40
    final double statusBarHeight = MediaQuery.of(context).padding.top;
41 42 43 44 45 46 47 48
    return new TabBarSelection<_Page>(
      values: _pages,
      onChanged: (_Page value) {
        setState(() {
          _selectedPage = value;
          _selectedPage.key.currentState.scrollTo(_scrollOffset);
        });
      },
49
      child: new Scaffold(
50
        appBarBehavior: AppBarBehavior.under,
51
        appBar: new AppBar(
52
          title: new Text('Tabs and scrolling'),
53 54 55 56
          tabBar: new TabBar<_Page>(
            labels: new Map<_Page, TabLabel>.fromIterable(_pages, value: (_Page page) {
              return new TabLabel(text: page.label);
            })
Hans Muller's avatar
Hans Muller committed
57
          )
58
        ),
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        body: new TabBarView<_Page>(
          children: _pages.map((_Page page) {
            return new Block(
              padding: new EdgeInsets.only(top: kTextTabBarHeight + kToolBarHeight + statusBarHeight),
              scrollableKey: page.key,
              onScroll: (double value) { _scrollOffset = value; },
              children: new List<Widget>.generate(6, (int i) {
                return new Container(
                  padding: const EdgeInsets.all(8.0),
                  height: 192.0,
                  child: new Card(
                    child: new Center(
                      child: new Text('Tab $page.label, item $i')
                    )
73
                  )
74 75
                );
              })
76 77 78 79
            );
          }).toList()
        )
      )
80 81 82
    );
  }
}