leave_behind_demo.dart 5.52 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 'package:collection/collection.dart' show lowerBound;

7 8 9 10 11 12 13 14 15
import 'package:flutter/material.dart';

enum LeaveBehindDemoAction {
  reset,
  horizontalSwipe,
  leftSwipe,
  rightSwipe
}

Adam Barth's avatar
Adam Barth committed
16
class LeaveBehindItem implements Comparable<LeaveBehindItem> {
17 18 19 20 21 22 23 24 25
  LeaveBehindItem({ this.index, this.name, this.subject, this.body });

  LeaveBehindItem.from(LeaveBehindItem item)
    : index = item.index, name = item.name, subject = item.subject, body = item.body;

  final int index;
  final String name;
  final String subject;
  final String body;
Adam Barth's avatar
Adam Barth committed
26 27 28

  @override
  int compareTo(LeaveBehindItem other) => index.compareTo(other.index);
29 30
}

31
class LeaveBehindDemo extends StatefulWidget {
32 33
  LeaveBehindDemo({ Key key }) : super(key: key);

34 35
  static const String routeName = '/leave-behind';

36
  @override
37 38 39 40
  LeaveBehindDemoState createState() => new LeaveBehindDemoState();
}

class LeaveBehindDemoState extends State<LeaveBehindDemo> {
41 42
  static final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  static final GlobalKey<ScrollableState> _scrollableKey = new GlobalKey<ScrollableState>();
43 44 45 46
  DismissDirection _dismissDirection = DismissDirection.horizontal;
  List<LeaveBehindItem> leaveBehindItems;

  void initListItems() {
47
    leaveBehindItems = new List<LeaveBehindItem>.generate(16, (int index) {
48 49 50 51 52 53 54 55 56
      return new LeaveBehindItem(
        index: index,
        name: 'Item $index Sender',
        subject: 'Subject: $index',
        body: "[$index] first line of the message's body..."
      );
    });
  }

57
  @override
58 59 60 61 62 63 64 65 66 67 68 69 70 71
  void initState() {
    super.initState();
    initListItems();
  }

  void handleDemoAction(LeaveBehindDemoAction action) {
    switch(action) {
      case LeaveBehindDemoAction.reset:
        initListItems();
        break;
      case LeaveBehindDemoAction.horizontalSwipe:
        _dismissDirection = DismissDirection.horizontal;
        break;
      case LeaveBehindDemoAction.leftSwipe:
72
        _dismissDirection = DismissDirection.endToStart;
73 74
        break;
      case LeaveBehindDemoAction.rightSwipe:
75
        _dismissDirection = DismissDirection.startToEnd;
76 77 78 79
        break;
    }
  }

80
  void handleUndo(LeaveBehindItem item) {
Adam Barth's avatar
Adam Barth committed
81
    int insertionIndex = lowerBound(leaveBehindItems, item);
82 83 84 85 86
    setState(() {
      leaveBehindItems.insert(insertionIndex, item);
    });
  }

87 88 89 90 91 92 93 94 95
  Widget buildItem(LeaveBehindItem item) {
    final ThemeData theme = Theme.of(context);
    return new Dismissable(
      key: new ObjectKey(item),
      direction: _dismissDirection,
      onDismissed: (DismissDirection direction) {
        setState(() {
          leaveBehindItems.remove(item);
        });
96
        final String action = (direction == DismissDirection.endToStart) ? 'archived' : 'deleted';
97
        _scaffoldKey.currentState.showSnackBar(new SnackBar(
98 99 100 101 102
          content: new Text('You $action item ${item.index}'),
          action: new SnackBarAction(
            label: 'UNDO',
            onPressed: () { handleUndo(item); }
          )
103 104 105 106 107
        ));
      },
      background: new Container(
        decoration: new BoxDecoration(backgroundColor: theme.primaryColor),
        child: new ListItem(
Ian Hickson's avatar
Ian Hickson committed
108
          leading: new Icon(Icons.delete, color: Colors.white, size: 36.0)
109 110 111 112 113
        )
      ),
      secondaryBackground: new Container(
        decoration: new BoxDecoration(backgroundColor: theme.primaryColor),
        child: new ListItem(
Ian Hickson's avatar
Ian Hickson committed
114
          trailing: new Icon(Icons.archive, color: Colors.white, size: 36.0)
115 116 117 118 119 120 121 122
        )
      ),
      child: new Container(
        decoration: new BoxDecoration(
          backgroundColor: theme.canvasColor,
          border: new Border(bottom: new BorderSide(color: theme.dividerColor))
        ),
        child: new ListItem(
123 124
          title: new Text(item.name),
          subtitle: new Text('${item.subject}\n${item.body}'),
125 126 127 128 129 130
          isThreeLine: true
        )
      )
    );
  }

131
  @override
132 133 134
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
135
      scrollableKey: _scrollableKey,
136
      appBar: new AppBar(
137
        title: new Text('Swipe items to dismiss'),
138
        actions: <Widget>[
139 140
          new PopupMenuButton<LeaveBehindDemoAction>(
            onSelected: handleDemoAction,
141
            itemBuilder: (BuildContext context) => <PopupMenuEntry<LeaveBehindDemoAction>>[
142 143 144 145 146 147 148 149 150 151 152 153
              new PopupMenuItem<LeaveBehindDemoAction>(
                value: LeaveBehindDemoAction.reset,
                child: new Text('Reset the list')
              ),
              new PopupMenuDivider(),
              new CheckedPopupMenuItem<LeaveBehindDemoAction>(
                value: LeaveBehindDemoAction.horizontalSwipe,
                checked: _dismissDirection == DismissDirection.horizontal,
                child: new Text('Hoizontal swipe')
              ),
              new CheckedPopupMenuItem<LeaveBehindDemoAction>(
                value: LeaveBehindDemoAction.leftSwipe,
154
                checked: _dismissDirection == DismissDirection.endToStart,
155 156 157 158
                child: new Text('Only swipe left')
              ),
              new CheckedPopupMenuItem<LeaveBehindDemoAction>(
                value: LeaveBehindDemoAction.rightSwipe,
159
                checked: _dismissDirection == DismissDirection.startToEnd,
160 161 162 163 164 165
                child: new Text('Only swipe right')
              )
            ]
          )
        ]
      ),
166 167 168 169
      body: new Block(
        scrollableKey: _scrollableKey,
        children: leaveBehindItems.map(buildItem).toList()
      )
170 171 172
    );
  }
}