leave_behind_demo.dart 5.35 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
  @override
35 36 37 38 39 40 41 42 43
  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() {
44
    leaveBehindItems = new List<LeaveBehindItem>.generate(16, (int index) {
45 46 47 48 49 50 51 52 53
      return new LeaveBehindItem(
        index: index,
        name: 'Item $index Sender',
        subject: 'Subject: $index',
        body: "[$index] first line of the message's body..."
      );
    });
  }

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

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

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

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