overscroll_demo.dart 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2016 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 'dart:async';

import 'package:flutter/material.dart';

enum IndicatorType { overscroll, refresh }

class OverscrollDemo extends StatefulWidget {
12
  const OverscrollDemo({ Key key }) : super(key: key);
13

14
  static const String routeName = '/material/overscroll';
15

16 17 18 19 20
  @override
  OverscrollDemoState createState() => new OverscrollDemoState();
}

class OverscrollDemoState extends State<OverscrollDemo> {
21 22
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
23 24 25 26
  static final List<String> _items = <String>[
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'
  ];

27
  Future<Null> _handleRefresh() {
28
    final Completer<Null> completer = new Completer<Null>();
29
    new Timer(const Duration(seconds: 3), () { completer.complete(null); });
30
    return completer.future.then((_) {
Hans Muller's avatar
Hans Muller committed
31
       _scaffoldKey.currentState?.showSnackBar(new SnackBar(
32
         content: const Text('Refresh complete'),
Hans Muller's avatar
Hans Muller committed
33 34 35 36 37 38 39
         action: new SnackBarAction(
           label: 'RETRY',
           onPressed: () {
             _refreshIndicatorKey.currentState.show();
           }
         )
       ));
40
    });
41 42 43 44 45
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
46
      key: _scaffoldKey,
47
      appBar: new AppBar(
48
        title: const Text('Pull to refresh'),
49 50
        actions: <Widget>[
          new IconButton(
51
            icon: const Icon(Icons.refresh),
52
            tooltip: 'Refresh',
53
            onPressed: () {
54
              _refreshIndicatorKey.currentState.show();
55 56 57 58
            }
          ),
        ]
      ),
59 60 61 62
      body: new RefreshIndicator(
        key: _refreshIndicatorKey,
        onRefresh: _handleRefresh,
        child: new ListView.builder(
63
          padding: kMaterialListPadding,
64 65
          itemCount: _items.length,
          itemBuilder: (BuildContext context, int index) {
66
            final String item = _items[index];
67
            return new ListTile(
68 69 70
              isThreeLine: true,
              leading: new CircleAvatar(child: new Text(item)),
              title: new Text('This item represents $item.'),
71
              subtitle: const Text('Even more additional list item information appears on line three.'),
72 73 74 75
            );
          },
        ),
      ),
76 77 78
    );
  }
}