overscroll_demo.dart 2.51 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
      if (!mounted) {
33
        return;
34
      }
35
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
36 37 38 39
        content: const Text('Refresh complete'),
        action: SnackBarAction(
          label: 'RETRY',
          onPressed: () {
40
            _refreshIndicatorKey.currentState!.show();
41 42 43
          },
        ),
      ));
44
    });
45 46 47 48
  }

  @override
  Widget build(BuildContext context) {
49 50
    return Scaffold(
      appBar: AppBar(
51
        title: const Text('Pull to refresh'),
52
        actions: <Widget>[
53
          MaterialDemoDocumentationButton(OverscrollDemo.routeName),
54
          IconButton(
55
            icon: const Icon(Icons.refresh),
56
            tooltip: 'Refresh',
57
            onPressed: () {
58
              _refreshIndicatorKey.currentState!.show();
59
            },
60
          ),
61
        ],
62
      ),
63
      body: RefreshIndicator(
64 65
        key: _refreshIndicatorKey,
        onRefresh: _handleRefresh,
66 67
        child: Scrollbar(
          child: ListView.builder(
68
            primary: true,
69 70 71 72 73 74 75 76 77 78 79 80
            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.'),
              );
            },
          ),
81 82
        ),
      ),
83 84 85
    );
  }
}