leave_behind_demo.dart 5.33 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 41 42 43 44 45
  LeaveBehindDemoState createState() => new LeaveBehindDemoState();
}

class LeaveBehindDemoState extends State<LeaveBehindDemo> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  DismissDirection _dismissDirection = DismissDirection.horizontal;
  List<LeaveBehindItem> leaveBehindItems;

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

56
  @override
57 58 59 60 61 62 63 64 65 66 67 68 69 70
  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:
71
        _dismissDirection = DismissDirection.endToStart;
72 73
        break;
      case LeaveBehindDemoAction.rightSwipe:
74
        _dismissDirection = DismissDirection.startToEnd;
75 76 77 78
        break;
    }
  }

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

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

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