overscroll_demo.dart 2.47 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// 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';

9 10
import '../../gallery/demo.dart';

11 12 13
enum IndicatorType { overscroll, refresh }

class OverscrollDemo extends StatefulWidget {
14
  const OverscrollDemo({ super.key });
15

16
  static const String routeName = '/material/overscroll';
17

18
  @override
19
  OverscrollDemoState createState() => OverscrollDemoState();
20 21 22
}

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

28 29
  Future<void> _handleRefresh() {
    final Completer<void> completer = Completer<void>();
30
    Timer(const Duration(seconds: 3), () => completer.complete());
31
    return completer.future.then((_) {
32 33
      if (!mounted)
        return;
34
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
35 36 37 38
        content: const Text('Refresh complete'),
        action: SnackBarAction(
          label: 'RETRY',
          onPressed: () {
39
            _refreshIndicatorKey.currentState!.show();
40 41 42
          },
        ),
      ));
43
    });
44 45 46 47
  }

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