cupertino_refresh_demo.dart 8.68 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2018 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 'dart:math' show Random;

import 'package:flutter/cupertino.dart';

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

11 12 13 14
class CupertinoRefreshControlDemo extends StatefulWidget {
  static const String routeName = '/cupertino/refresh';

  @override
15
  _CupertinoRefreshControlDemoState createState() => _CupertinoRefreshControlDemoState();
16 17 18 19 20 21 22 23 24 25 26 27
}

class _CupertinoRefreshControlDemoState extends State<CupertinoRefreshControlDemo> {
  List<List<String>> randomizedContacts;

  @override
  void initState() {
    super.initState();
    repopulateList();
  }

  void repopulateList() {
28 29
    final Random random = Random();
    randomizedContacts = List<List<String>>.generate(
30 31 32 33 34
      100,
      (int index) {
        return contacts[random.nextInt(contacts.length)]
            // Randomly adds a telephone icon next to the contact or not.
            ..add(random.nextBool().toString());
35
      },
36 37 38 39 40
    );
  }

  @override
  Widget build(BuildContext context) {
41
    return DefaultTextStyle(
xster's avatar
xster committed
42
      style: CupertinoTheme.of(context).textTheme.textStyle,
43 44
      child: CupertinoPageScaffold(
        child: DecoratedBox(
xster's avatar
xster committed
45 46 47 48 49
          decoration: BoxDecoration(
            color: CupertinoTheme.of(context).brightness == Brightness.light
                ? CupertinoColors.extraLightBackgroundGray
                : CupertinoColors.darkBackgroundGray,
          ),
50
          child: CustomScrollView(
51
            slivers: <Widget>[
52
              CupertinoSliverNavigationBar(
xster's avatar
xster committed
53 54 55 56
                largeTitle: const Text('Refresh'),
                // We're specifying a back label here because the previous page
                // is a Material page. CupertinoPageRoutes could auto-populate
                // these back labels.
57
                previousPageTitle: 'Cupertino',
58
                trailing: CupertinoDemoDocumentationButton(CupertinoRefreshControlDemo.routeName),
59
              ),
60
              CupertinoSliverRefreshControl(
61
                onRefresh: () {
62
                  return Future<void>.delayed(const Duration(seconds: 2))
63
                      ..then<void>((_) {
64 65 66 67
                        if (mounted) {
                          setState(() => repopulateList());
                        }
                      });
68 69
                },
              ),
70
              SliverSafeArea(
71
                top: false, // Top safe area is consumed by the navigation bar.
72 73
                sliver: SliverList(
                  delegate: SliverChildBuilderDelegate(
74
                    (BuildContext context, int index) {
75
                      return _ListItem(
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                        name: randomizedContacts[index][0],
                        place: randomizedContacts[index][1],
                        date: randomizedContacts[index][2],
                        called: randomizedContacts[index][3] == 'true',
                      );
                    },
                    childCount: 20,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

List<List<String>> contacts = <List<String>>[
  <String>['George Washington', 'Westmoreland County', ' 4/30/1789'],
  <String>['John Adams', 'Braintree', ' 3/4/1797'],
  <String>['Thomas Jefferson', 'Shadwell', ' 3/4/1801'],
  <String>['James Madison', 'Port Conway', ' 3/4/1809'],
  <String>['James Monroe', 'Monroe Hall', ' 3/4/1817'],
  <String>['Andrew Jackson', 'Waxhaws Region South/North', ' 3/4/1829'],
  <String>['John Quincy Adams', 'Braintree', ' 3/4/1825'],
  <String>['William Henry Harrison', 'Charles City County', ' 3/4/1841'],
  <String>['Martin Van Buren', 'Kinderhook New', ' 3/4/1837'],
  <String>['Zachary Taylor', 'Barboursville', ' 3/4/1849'],
  <String>['John Tyler', 'Charles City County', ' 4/4/1841'],
  <String>['James Buchanan', 'Cove Gap', ' 3/4/1857'],
  <String>['James K. Polk', 'Pineville North', ' 3/4/1845'],
  <String>['Millard Fillmore', 'Summerhill New', '7/9/1850'],
  <String>['Franklin Pierce', 'Hillsborough New', ' 3/4/1853'],
  <String>['Andrew Johnson', 'Raleigh North', ' 4/15/1865'],
  <String>['Abraham Lincoln', 'Sinking Spring', ' 3/4/1861'],
  <String>['Ulysses S. Grant', 'Point Pleasant', ' 3/4/1869'],
  <String>['Rutherford B. Hayes', 'Delaware', ' 3/4/1877'],
  <String>['Chester A. Arthur', 'Fairfield', ' 9/19/1881'],
  <String>['James A. Garfield', 'Moreland Hills', ' 3/4/1881'],
  <String>['Benjamin Harrison', 'North Bend', ' 3/4/1889'],
  <String>['Grover Cleveland', 'Caldwell New', ' 3/4/1885'],
  <String>['William McKinley', 'Niles', ' 3/4/1897'],
  <String>['Woodrow Wilson', 'Staunton', ' 3/4/1913'],
  <String>['William H. Taft', 'Cincinnati', ' 3/4/1909'],
  <String>['Theodore Roosevelt', 'New York City New', ' 9/14/1901'],
  <String>['Warren G. Harding', 'Blooming Grove', ' 3/4/1921'],
  <String>['Calvin Coolidge', 'Plymouth', '8/2/1923'],
  <String>['Herbert Hoover', 'West Branch', ' 3/4/1929'],
  <String>['Franklin D. Roosevelt', 'Hyde Park New', ' 3/4/1933'],
  <String>['Harry S. Truman', 'Lamar', ' 4/12/1945'],
  <String>['Dwight D. Eisenhower', 'Denison', ' 1/20/1953'],
  <String>['Lyndon B. Johnson', 'Stonewall', '11/22/1963'],
  <String>['Ronald Reagan', 'Tampico', ' 1/20/1981'],
  <String>['Richard Nixon', 'Yorba Linda', ' 1/20/1969'],
  <String>['Gerald Ford', 'Omaha', 'August 9/1974'],
  <String>['John F. Kennedy', 'Brookline', ' 1/20/1961'],
  <String>['George H. W. Bush', 'Milton', ' 1/20/1989'],
  <String>['Jimmy Carter', 'Plains', ' 1/20/1977'],
  <String>['George W. Bush', 'New Haven', ' 1/20, 2001'],
  <String>['Bill Clinton', 'Hope', ' 1/20/1993'],
  <String>['Barack Obama', 'Honolulu', ' 1/20/2009'],
  <String>['Donald J. Trump', 'New York City', ' 1/20/2017'],
];

class _ListItem extends StatelessWidget {
  const _ListItem({
    this.name,
    this.place,
    this.date,
    this.called,
  });

  final String name;
  final String place;
  final String date;
  final bool called;

  @override
  Widget build(BuildContext context) {
156
    return Container(
xster's avatar
xster committed
157
      color: CupertinoTheme.of(context).scaffoldBackgroundColor,
158 159
      height: 60.0,
      padding: const EdgeInsets.only(top: 9.0),
160
      child: Row(
161
        children: <Widget>[
162
          Container(
163 164
            width: 38.0,
            child: called
165
                ? const Align(
166
                    alignment: Alignment.topCenter,
167
                    child: Icon(
168 169 170 171 172 173 174
                      CupertinoIcons.phone_solid,
                      color: CupertinoColors.inactiveGray,
                      size: 18.0,
                    ),
                  )
                : null,
          ),
175 176
        Expanded(
          child: Container(
177
              decoration: const BoxDecoration(
178 179
                border: Border(
                  bottom: BorderSide(color: Color(0xFFBCBBC1), width: 0.0),
180 181 182
                ),
              ),
              padding: const EdgeInsets.only(left: 1.0, bottom: 9.0, right: 10.0),
183
              child: Row(
184
                children: <Widget>[
185 186
                  Expanded(
                    child: Column(
187 188 189
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
190
                        Text(
191 192 193 194 195
                          name,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: const TextStyle(
                            fontWeight: FontWeight.w600,
196
                            letterSpacing: -0.18,
197 198
                          ),
                        ),
199
                        Text(
200 201 202 203 204 205 206 207 208 209 210 211
                          place,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: const TextStyle(
                            fontSize: 15.0,
                            letterSpacing: -0.24,
                            color: CupertinoColors.inactiveGray,
                          ),
                        ),
                      ],
                    ),
                  ),
212
                  Text(
213 214 215 216 217 218 219
                    date,
                    style: const TextStyle(
                      color: CupertinoColors.inactiveGray,
                      fontSize: 15.0,
                      letterSpacing: -0.41,
                    ),
                  ),
xster's avatar
xster committed
220 221
                  Padding(
                    padding: const EdgeInsets.only(left: 9.0),
222
                    child: Icon(
223
                      CupertinoIcons.info,
xster's avatar
xster committed
224
                      color: CupertinoTheme.of(context).primaryColor,
225 226 227 228 229 230 231 232 233 234 235
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}