cupertino_sliver_refresh_control.0.dart 2.31 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter 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/cupertino.dart';

7 8
/// Flutter code sample for [CupertinoSliverRefreshControl].

9
void main() => runApp(const RefreshControlApp());
10

11
class RefreshControlApp extends StatelessWidget {
12
  const RefreshControlApp({super.key});
13 14 15 16

  @override
  Widget build(BuildContext context) {
    return const CupertinoApp(
17 18
      theme: CupertinoThemeData(brightness: Brightness.light),
      home: RefreshControlExample(),
19 20 21 22
    );
  }
}

23
class RefreshControlExample extends StatefulWidget {
24
  const RefreshControlExample({super.key});
25 26

  @override
27
  State<RefreshControlExample> createState() => _RefreshControlExampleState();
28 29
}

30
class _RefreshControlExampleState extends State<RefreshControlExample> {
31 32 33
  List<Color> colors = <Color>[
    CupertinoColors.systemYellow,
    CupertinoColors.systemOrange,
34
    CupertinoColors.systemPink,
35 36 37 38 39 40 41 42 43
  ];
  List<Widget> items = <Widget>[
    Container(color: CupertinoColors.systemPink, height: 100.0),
    Container(color: CupertinoColors.systemOrange, height: 100.0),
    Container(color: CupertinoColors.systemYellow, height: 100.0),
  ];

  @override
  Widget build(BuildContext context) {
44
    return CupertinoPageScaffold(
45 46 47
      navigationBar: const CupertinoNavigationBar(
        middle: Text('CupertinoSliverRefreshControl Sample'),
      ),
48 49 50
      child: CustomScrollView(
        physics: const BouncingScrollPhysics(
          parent: AlwaysScrollableScrollPhysics(),
51
        ),
52 53 54
        slivers: <Widget>[
          const CupertinoSliverNavigationBar(
            largeTitle: Text('Scroll down'),
55
          ),
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
          CupertinoSliverRefreshControl(
            onRefresh: () async {
              await Future<void>.delayed(
                const Duration(milliseconds: 1000),
              );
              setState(() {
                items.insert(
                  0,
                  Container(color: colors[items.length % 3], height: 100.0),
                );
              });
            },
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) => items[index],
              childCount: items.length,
            ),
          ),
        ],
      ),
    );
78 79
  }
}