paginated_data_table.dart 15.9 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'dart:math' as math;

7
import 'package:flutter/widgets.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter/gestures.dart' show DragStartBehavior;
10

11
import 'button_bar.dart';
12
import 'button_theme.dart';
13 14 15
import 'card.dart';
import 'data_table.dart';
import 'data_table_source.dart';
16
import 'debug.dart';
17
import 'dropdown.dart';
18 19
import 'icon_button.dart';
import 'icons.dart';
20
import 'ink_decoration.dart';
21
import 'material_localizations.dart';
22
import 'progress_indicator.dart';
23
import 'theme.dart';
24

Adam Barth's avatar
Adam Barth committed
25 26 27
/// A material design data table that shows data using multiple pages.
///
/// A paginated data table shows [rowsPerPage] rows of data per page and
28
/// provides controls for showing other pages.
Adam Barth's avatar
Adam Barth committed
29 30 31 32 33 34 35
///
/// Data is read lazily from from a [DataTableSource]. The widget is presented
/// as a [Card].
///
/// See also:
///
///  * [DataTable], which is not paginated.
36
///  * <https://material.io/go/design-data-tables#data-tables-tables-within-cards>
37 38 39
class PaginatedDataTable extends StatefulWidget {
  /// Creates a widget describing a paginated [DataTable] on a [Card].
  ///
40 41 42
  /// The [header] should give the card's header, typically a [Text] widget. It
  /// must not be null.
  ///
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  /// The [columns] argument must be a list of as many [DataColumn] objects as
  /// the table is to have columns, ignoring the leading checkbox column if any.
  /// The [columns] argument must have a length greater than zero and cannot be
  /// null.
  ///
  /// If the table is sorted, the column that provides the current primary key
  /// should be specified by index in [sortColumnIndex], 0 meaning the first
  /// column in [columns], 1 being the next one, and so forth.
  ///
  /// The actual sort order can be specified using [sortAscending]; if the sort
  /// order is ascending, this should be true (the default), otherwise it should
  /// be false.
  ///
  /// The [source] must not be null. The [source] should be a long-lived
  /// [DataTableSource]. The same source should be provided each time a
  /// particular [PaginatedDataTable] widget is created; avoid creating a new
  /// [DataTableSource] with each new instance of the [PaginatedDataTable]
  /// widget unless the data table really is to now show entirely different
  /// data from a new source.
62
  ///
63 64
  /// The [rowsPerPage] and [availableRowsPerPage] must not be null (they
  /// both have defaults, though, so don't have to be specified).
65 66
  PaginatedDataTable({
    Key key,
67 68
    @required this.header,
    this.actions,
69
    @required this.columns,
70
    this.sortColumnIndex,
71
    this.sortAscending = true,
72
    this.onSelectAll,
73
    this.initialFirstRowIndex = 0,
74
    this.onPageChanged,
75 76
    this.rowsPerPage = defaultRowsPerPage,
    this.availableRowsPerPage = const <int>[defaultRowsPerPage, defaultRowsPerPage * 2, defaultRowsPerPage * 5, defaultRowsPerPage * 10],
77
    this.onRowsPerPageChanged,
78
    this.dragStartBehavior = DragStartBehavior.start,
79
    @required this.source,
80 81
  }) : assert(header != null),
       assert(columns != null),
82
       assert(dragStartBehavior != null),
83 84 85 86 87 88 89 90 91
       assert(columns.isNotEmpty),
       assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)),
       assert(sortAscending != null),
       assert(rowsPerPage != null),
       assert(rowsPerPage > 0),
       assert(() {
         if (onRowsPerPageChanged != null)
           assert(availableRowsPerPage != null && availableRowsPerPage.contains(rowsPerPage));
         return true;
92
       }()),
93 94
       assert(source != null),
       super(key: key);
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  /// The table card's header.
  ///
  /// This is typically a [Text] widget, but can also be a [ButtonBar] with
  /// [FlatButton]s. Suitable defaults are automatically provided for the font,
  /// button color, button padding, and so forth.
  ///
  /// If items in the table are selectable, then, when the selection is not
  /// empty, the header is replaced by a count of the selected items.
  final Widget header;

  /// Icon buttons to show at the top right of the table.
  ///
  /// Typically, the exact actions included in this list will vary based on
  /// whether any rows are selected or not.
  ///
  /// These should be size 24.0 with default padding (8.0).
  final List<Widget> actions;

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  /// The configuration and labels for the columns in the table.
  final List<DataColumn> columns;

  /// The current primary sort key's column.
  ///
  /// See [DataTable.sortColumnIndex].
  final int sortColumnIndex;

  /// Whether the column mentioned in [sortColumnIndex], if any, is sorted
  /// in ascending order.
  ///
  /// See [DataTable.sortAscending].
  final bool sortAscending;

  /// Invoked when the user selects or unselects every row, using the
  /// checkbox in the heading row.
  ///
  /// See [DataTable.onSelectAll].
  final ValueSetter<bool> onSelectAll;

  /// The index of the first row to display when the widget is first created.
  final int initialFirstRowIndex;

  /// Invoked when the user switches to another page.
138 139
  ///
  /// The value is the index of the first row on the currently displayed page.
140 141 142 143
  final ValueChanged<int> onPageChanged;

  /// The number of rows to show on each page.
  ///
144 145
  /// See also:
  ///
146 147
  ///  * [onRowsPerPageChanged]
  ///  * [defaultRowsPerPage]
148 149
  final int rowsPerPage;

150 151 152 153 154 155 156 157 158 159 160 161 162
  /// The default value for [rowsPerPage].
  ///
  /// Useful when initializing the field that will hold the current
  /// [rowsPerPage], when implemented [onRowsPerPageChanged].
  static const int defaultRowsPerPage = 10;

  /// The options to offer for the rowsPerPage.
  ///
  /// The current [rowsPerPage] must be a value in this list.
  ///
  /// The values in this list should be sorted in ascending order.
  final List<int> availableRowsPerPage;

163 164 165 166 167 168 169 170 171 172 173 174 175
  /// Invoked when the user selects a different number of rows per page.
  ///
  /// If this is null, then the value given by [rowsPerPage] will be used
  /// and no affordance will be provided to change the value.
  final ValueChanged<int> onRowsPerPageChanged;

  /// The data source which provides data to show in each row. Must be non-null.
  ///
  /// This object should generally have a lifetime longer than the
  /// [PaginatedDataTable] widget itself; it should be reused each time the
  /// [PaginatedDataTable] constructor is called.
  final DataTableSource source;

176 177 178
  /// {@macro flutter.widgets.scrollable.dragStartBehavior}
  final DragStartBehavior dragStartBehavior;

179
  @override
180
  PaginatedDataTableState createState() => PaginatedDataTableState();
181 182 183 184 185 186 187 188 189
}

/// Holds the state of a [PaginatedDataTable].
///
/// The table can be programmatically paged using the [pageTo] method.
class PaginatedDataTableState extends State<PaginatedDataTable> {
  int _firstRowIndex;
  int _rowCount;
  bool _rowCountApproximate;
190
  int _selectedRowCount;
191 192 193 194 195
  final Map<int, DataRow> _rows = <int, DataRow>{};

  @override
  void initState() {
    super.initState();
196 197
    _firstRowIndex = PageStorage.of(context)?.readState(context) ?? widget.initialFirstRowIndex ?? 0;
    widget.source.addListener(_handleDataSourceChanged);
198 199 200 201
    _handleDataSourceChanged();
  }

  @override
202 203 204 205 206
  void didUpdateWidget(PaginatedDataTable oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.source != widget.source) {
      oldWidget.source.removeListener(_handleDataSourceChanged);
      widget.source.addListener(_handleDataSourceChanged);
207 208 209 210 211 212
      _handleDataSourceChanged();
    }
  }

  @override
  void dispose() {
213
    widget.source.removeListener(_handleDataSourceChanged);
214 215 216 217 218
    super.dispose();
  }

  void _handleDataSourceChanged() {
    setState(() {
219 220 221
      _rowCount = widget.source.rowCount;
      _rowCountApproximate = widget.source.isRowCountApproximate;
      _selectedRowCount = widget.source.selectedRowCount;
222 223 224 225 226
      _rows.clear();
    });
  }

  /// Ensures that the given row is visible.
227 228
  void pageTo(int rowIndex) {
    final int oldFirstRowIndex = _firstRowIndex;
229
    setState(() {
230
      final int rowsPerPage = widget.rowsPerPage;
231 232
      _firstRowIndex = (rowIndex ~/ rowsPerPage) * rowsPerPage;
    });
233
    if ((widget.onPageChanged != null) &&
234
        (oldFirstRowIndex != _firstRowIndex))
235
      widget.onPageChanged(_firstRowIndex);
236 237 238
  }

  DataRow _getBlankRowFor(int index) {
239
    return DataRow.byIndex(
240
      index: index,
241
      cells: widget.columns.map<DataCell>((DataColumn column) => DataCell.empty).toList(),
242 243 244 245 246
    );
  }

  DataRow _getProgressIndicatorRowFor(int index) {
    bool haveProgressIndicator = false;
247
    final List<DataCell> cells = widget.columns.map<DataCell>((DataColumn column) {
248 249
      if (!column.numeric) {
        haveProgressIndicator = true;
250
        return const DataCell(CircularProgressIndicator());
251 252 253 254 255
      }
      return DataCell.empty;
    }).toList();
    if (!haveProgressIndicator) {
      haveProgressIndicator = true;
256
      cells[0] = const DataCell(CircularProgressIndicator());
257
    }
258
    return DataRow.byIndex(
259
      index: index,
260
      cells: cells,
261 262 263 264 265 266 267 268 269 270
    );
  }

  List<DataRow> _getRows(int firstRowIndex, int rowsPerPage) {
    final List<DataRow> result = <DataRow>[];
    final int nextPageFirstRowIndex = firstRowIndex + rowsPerPage;
    bool haveProgressIndicator = false;
    for (int index = firstRowIndex; index < nextPageFirstRowIndex; index += 1) {
      DataRow row;
      if (index < _rowCount || _rowCountApproximate) {
271
        row = _rows.putIfAbsent(index, () => widget.source.getRow(index));
272 273 274 275 276 277 278 279 280 281 282
        if (row == null && !haveProgressIndicator) {
          row ??= _getProgressIndicatorRowFor(index);
          haveProgressIndicator = true;
        }
      }
      row ??= _getBlankRowFor(index);
      result.add(row);
    }
    return result;
  }

283 284 285 286 287 288 289 290
  void _handlePrevious() {
    pageTo(math.max(_firstRowIndex - widget.rowsPerPage, 0));
  }

  void _handleNext() {
    pageTo(_firstRowIndex + widget.rowsPerPage);
  }

291
  final GlobalKey _tableKey = GlobalKey();
292 293 294

  @override
  Widget build(BuildContext context) {
295
    // TODO(ianh): This whole build function doesn't handle RTL yet.
296
    assert(debugCheckHasMaterialLocalizations(context));
297
    final ThemeData themeData = Theme.of(context);
298
    final MaterialLocalizations localizations = MaterialLocalizations.of(context);
299 300
    // HEADER
    final List<Widget> headerWidgets = <Widget>[];
301
    double startPadding = 24.0;
302
    if (_selectedRowCount == 0) {
303
      headerWidgets.add(Expanded(child: widget.header));
304
      if (widget.header is ButtonBar) {
305 306 307 308 309 310
        // We adjust the padding when a button bar is present, because the
        // ButtonBar introduces 2 pixels of outside padding, plus 2 pixels
        // around each button on each side, and the button itself will have 8
        // pixels internally on each side, yet we want the left edge of the
        // inside of the button to line up with the 24.0 left inset.
        // TODO(ianh): Better magic. See https://github.com/flutter/flutter/issues/4460
311
        startPadding = 12.0;
312 313
      }
    } else {
314 315
      headerWidgets.add(Expanded(
        child: Text(localizations.selectedRowCountTitle(_selectedRowCount)),
316
      ));
317
    }
318
    if (widget.actions != null) {
319
      headerWidgets.addAll(
320
        widget.actions.map<Widget>((Widget action) {
321
          return Padding(
322
            // 8.0 is the default padding of an icon button
323
            padding: const EdgeInsetsDirectional.only(start: 24.0 - 8.0 * 2.0),
324
            child: action,
325 326 327 328 329 330 331
          );
        }).toList()
      );
    }

    // FOOTER
    final TextStyle footerTextStyle = themeData.textTheme.caption;
332
    final List<Widget> footerWidgets = <Widget>[];
333 334
    if (widget.onRowsPerPageChanged != null) {
      final List<Widget> availableRowsPerPage = widget.availableRowsPerPage
335
        .where((int value) => value <= _rowCount || value == widget.rowsPerPage)
336
        .map<DropdownMenuItem<int>>((int value) {
337
          return DropdownMenuItem<int>(
338
            value: value,
339
            child: Text('$value'),
340 341 342 343
          );
        })
        .toList();
      footerWidgets.addAll(<Widget>[
344 345 346
        Container(width: 14.0), // to match trailing padding in case we overflow and end up scrolling
        Text(localizations.rowsPerPageTitle),
        ConstrainedBox(
347
          constraints: const BoxConstraints(minWidth: 64.0), // 40.0 for the text, 24.0 for the icon
348
          child: Align(
349
            alignment: AlignmentDirectional.centerEnd,
350 351
            child: DropdownButtonHideUnderline(
              child: DropdownButton<int>(
352 353 354 355 356 357 358 359
                items: availableRowsPerPage,
                value: widget.rowsPerPage,
                onChanged: widget.onRowsPerPageChanged,
                style: footerTextStyle,
                iconSize: 24.0,
              ),
            ),
          ),
360 361 362 363
        ),
      ]);
    }
    footerWidgets.addAll(<Widget>[
364 365
      Container(width: 32.0),
      Text(
366 367 368 369
        localizations.pageRowsInfoTitle(
          _firstRowIndex + 1,
          _firstRowIndex + widget.rowsPerPage,
          _rowCount,
370
          _rowCountApproximate,
371
        )
372
      ),
373 374
      Container(width: 32.0),
      IconButton(
375
        icon: const Icon(Icons.chevron_left),
376
        padding: EdgeInsets.zero,
377
        tooltip: localizations.previousPageTooltip,
378
        onPressed: _firstRowIndex <= 0 ? null : _handlePrevious,
379
      ),
380 381
      Container(width: 24.0),
      IconButton(
382
        icon: const Icon(Icons.chevron_right),
383
        padding: EdgeInsets.zero,
384
        tooltip: localizations.nextPageTooltip,
385
        onPressed: (!_rowCountApproximate && (_firstRowIndex + widget.rowsPerPage >= _rowCount)) ? null : _handleNext,
386
      ),
387
      Container(width: 14.0),
388
    ]);
389 390

    // CARD
391
    return Card(
392
      semanticContainer: false,
393
      child: Column(
394
        crossAxisAlignment: CrossAxisAlignment.stretch,
395
        children: <Widget>[
396
          Semantics(
397
            container: true,
398
            child: DefaultTextStyle(
399 400
              // These typographic styles aren't quite the regular ones. We pick the closest ones from the regular
              // list and then tweak them appropriately.
401
              // See https://material.io/design/components/data-tables.html#tables-within-cards
402 403 404 405 406 407
              style: _selectedRowCount > 0 ? themeData.textTheme.subhead.copyWith(color: themeData.accentColor)
                                           : themeData.textTheme.title.copyWith(fontWeight: FontWeight.w400),
              child: IconTheme.merge(
                data: const IconThemeData(
                  opacity: 0.54
                ),
408 409
                child: ButtonTheme.bar(
                  child: Ink(
410 411
                    height: 64.0,
                    color: _selectedRowCount > 0 ? themeData.secondaryHeaderColor : null,
412 413 414
                    child: Padding(
                      padding: EdgeInsetsDirectional.only(start: startPadding, end: 14.0),
                      child: Row(
415
                        mainAxisAlignment: MainAxisAlignment.end,
416
                        children: headerWidgets,
417 418 419 420 421
                      ),
                    ),
                  ),
                ),
              ),
422
            ),
423
          ),
424
          SingleChildScrollView(
425
            scrollDirection: Axis.horizontal,
426
            dragStartBehavior: widget.dragStartBehavior,
427
            child: DataTable(
428
              key: _tableKey,
429 430 431 432
              columns: widget.columns,
              sortColumnIndex: widget.sortColumnIndex,
              sortAscending: widget.sortAscending,
              onSelectAll: widget.onSelectAll,
433 434
              rows: _getRows(_firstRowIndex, widget.rowsPerPage),
            ),
435
          ),
436
          DefaultTextStyle(
437
            style: footerTextStyle,
438
            child: IconTheme.merge(
439
              data: const IconThemeData(
440 441
                opacity: 0.54
              ),
442
              child: Container(
443
                height: 56.0,
444
                child: SingleChildScrollView(
445
                  dragStartBehavior: widget.dragStartBehavior,
446 447
                  scrollDirection: Axis.horizontal,
                  reverse: true,
448
                  child: Row(
449 450 451 452 453 454 455 456
                    children: footerWidgets,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
457 458 459
    );
  }
}