tabs_demo.dart 2.35 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 22 23 24 25 26 27 28 29 30 31 32 33 34
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 {
  @override
  TabsDemoState createState() => new TabsDemoState();
}

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

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

36
  @override
37
  Widget build(BuildContext context) {
38 39 40 41 42 43 44 45 46
    final double statusBarHeight = (MediaQuery.of(context)?.padding ?? EdgeInsets.zero).top;
    return new TabBarSelection<_Page>(
      values: _pages,
      onChanged: (_Page value) {
        setState(() {
          _selectedPage = value;
          _selectedPage.key.currentState.scrollTo(_scrollOffset);
        });
      },
47
      child: new Scaffold(
48 49
        scrollableKey: _selectedPage.key,
        appBarBehavior: AppBarBehavior.under,
50
        appBar: new AppBar(
51 52 53 54 55
          title: new Text('Tabs and Scrolling'),
          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
56
          )
57
        ),
58 59 60 61 62 63 64 65 66 67 68 69 70 71
        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')
                    )
72
                  )
73 74
                );
              })
75 76 77 78
            );
          }).toList()
        )
      )
79 80 81
    );
  }
}