expansion_panels_demo.dart 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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.

import 'package:flutter/material.dart';

enum _Location {
  Barbados,
  Bahamas,
  Bermuda
}

13
typedef Widget DemoItemBodyBuilder<T>(DemoItem<T> item);
14 15 16
typedef String ValueToString<T>(T value);

class DualHeaderWithHint extends StatelessWidget {
17
  const DualHeaderWithHint({
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    this.name,
    this.value,
    this.hint,
    this.showHint
  });

  final String name;
  final String value;
  final String hint;
  final bool showHint;

  Widget _crossFade(Widget first, Widget second, bool isExpanded) {
    return new AnimatedCrossFade(
      firstChild: first,
      secondChild: second,
33 34
      firstCurve: const Interval(0.0, 0.6, curve: Curves.fastOutSlowIn),
      secondCurve: const Interval(0.4, 1.0, curve: Curves.fastOutSlowIn),
35 36 37 38 39 40 41 42
      sizeCurve: Curves.fastOutSlowIn,
      crossFadeState: isExpanded ? CrossFadeState.showSecond : CrossFadeState.showFirst,
      duration: const Duration(milliseconds: 200),
    );
  }

  @override
  Widget build(BuildContext context) {
43 44 45
    final ThemeData theme = Theme.of(context);
    final TextTheme textTheme = theme.textTheme;

46 47
    return new Row(
      children: <Widget>[
48
        new Expanded(
49 50 51
          flex: 2,
          child: new Container(
            margin: const EdgeInsets.only(left: 24.0),
52
            child: new FittedBox(
53
              fit: BoxFit.scaleDown,
54 55 56 57 58 59 60
              alignment: FractionalOffset.centerLeft,
              child: new Text(
                name,
                style: textTheme.body1.copyWith(fontSize: 15.0),
              ),
            ),
          ),
61
        ),
62
        new Expanded(
63 64 65 66
          flex: 3,
          child: new Container(
            margin: const EdgeInsets.only(left: 24.0),
            child: _crossFade(
67 68
              new Text(value, style: textTheme.caption.copyWith(fontSize: 15.0)),
              new Text(hint, style: textTheme.caption.copyWith(fontSize: 15.0)),
69 70 71 72 73 74 75 76 77 78
              showHint
            )
          )
        )
      ]
    );
  }
}

class CollapsibleBody extends StatelessWidget {
79
  const CollapsibleBody({
80 81 82 83 84 85 86 87 88 89 90 91 92
    this.margin: EdgeInsets.zero,
    this.child,
    this.onSave,
    this.onCancel
  });

  final EdgeInsets margin;
  final Widget child;
  final VoidCallback onSave;
  final VoidCallback onCancel;

  @override
  Widget build(BuildContext context) {
93 94 95
    final ThemeData theme = Theme.of(context);
    final TextTheme textTheme = theme.textTheme;

96 97 98 99 100 101 102 103 104 105
    return new Column(
      children: <Widget>[
        new Container(
          margin: const EdgeInsets.only(
            left: 24.0,
            right: 24.0,
            bottom: 24.0
          ) - margin,
          child: new Center(
            child: new DefaultTextStyle(
106
              style: textTheme.caption.copyWith(fontSize: 15.0),
107 108 109 110
              child: child
            )
          )
        ),
111
        const Divider(height: 1.0),
112 113 114 115 116 117 118 119 120
        new Container(
          padding: const EdgeInsets.symmetric(vertical: 16.0),
          child: new Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              new Container(
                margin: const EdgeInsets.only(right: 8.0),
                child: new FlatButton(
                  onPressed: onCancel,
121
                  child: const Text('CANCEL', style: const TextStyle(
122 123 124 125 126 127 128 129 130 131 132
                    color: Colors.black54,
                    fontSize: 15.0,
                    fontWeight: FontWeight.w500
                  ))
                )
              ),
              new Container(
                margin: const EdgeInsets.only(right: 8.0),
                child: new FlatButton(
                  onPressed: onSave,
                  textTheme: ButtonTextTheme.accent,
133
                  child: const Text('SAVE')
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                )
              )
            ]
          )
        )
      ]
    );
  }
}

class DemoItem<T> {
  DemoItem({
    this.name,
    this.value,
    this.hint,
    this.builder,
    this.valueToString
151
  }) : textController = new TextEditingController(text: valueToString(value));
152 153 154

  final String name;
  final String hint;
155
  final TextEditingController textController;
156
  final DemoItemBodyBuilder<T> builder;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  final ValueToString<T> valueToString;
  T value;
  bool isExpanded = false;

  ExpansionPanelHeaderBuilder get headerBuilder {
    return (BuildContext context, bool isExpanded) {
      return new DualHeaderWithHint(
        name: name,
        value: valueToString(value),
        hint: hint,
        showHint: isExpanded
      );
    };
  }
}

class ExpasionPanelsDemo extends StatefulWidget {
174
  static const String routeName = '/material/expansion_panels';
175 176 177 178 179 180 181 182 183 184 185 186 187 188

  @override
  _ExpansionPanelsDemoState createState() => new _ExpansionPanelsDemoState();
}

class _ExpansionPanelsDemoState extends State<ExpasionPanelsDemo> {
  List<DemoItem<dynamic>> _demoItems;

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

    _demoItems = <DemoItem<dynamic>>[
      new DemoItem<String>(
189
        name: 'Trip',
190 191 192
        value: 'Caribbean cruise',
        hint: 'Change trip name',
        valueToString: (String value) => value,
193
        builder: (DemoItem<String> item) {
194 195 196 197 198 199
          void close() {
            setState(() {
              item.isExpanded = false;
            });
          }

Matt Perry's avatar
Matt Perry committed
200 201 202 203 204 205 206 207 208
          return new Form(
            child: new Builder(
              builder: (BuildContext context) {
                return new CollapsibleBody(
                  margin: const EdgeInsets.symmetric(horizontal: 16.0),
                  onSave: () { Form.of(context).save(); close(); },
                  onCancel: () { Form.of(context).reset(); close(); },
                  child: new Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
209 210 211 212 213 214 215
                    child: new TextFormField(
                      controller: item.textController,
                      decoration: new InputDecoration(
                        hintText: item.hint,
                        labelText: item.name,
                      ),
                      onSaved: (String value) { item.value = value; },
Matt Perry's avatar
Matt Perry committed
216
                    ),
217
                  ),
Matt Perry's avatar
Matt Perry committed
218
                );
219 220
              },
            ),
221
          );
222
        },
223 224 225 226 227 228
      ),
      new DemoItem<_Location>(
        name: 'Location',
        value: _Location.Bahamas,
        hint: 'Select location',
        valueToString: (_Location location) => location.toString().split(".")[1],
229
        builder: (DemoItem<_Location> item) {
230 231 232 233 234
          void close() {
            setState(() {
              item.isExpanded = false;
            });
          }
Matt Perry's avatar
Matt Perry committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
          return new Form(
            child: new Builder(
              builder: (BuildContext context) {
                return new CollapsibleBody(
                  onSave: () { Form.of(context).save(); close(); },
                  onCancel: () { Form.of(context).reset(); close(); },
                  child: new FormField<_Location>(
                    initialValue: item.value,
                    onSaved: (_Location result) { item.value = result; },
                    builder: (FormFieldState<_Location> field) {
                      return new Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          new Row(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              new Radio<_Location>(
                                value: _Location.Bahamas,
                                groupValue: field.value,
                                onChanged: field.onChanged,
                              ),
257
                              const Text('Bahamas')
Matt Perry's avatar
Matt Perry committed
258 259 260 261 262 263 264 265 266 267
                            ]
                          ),
                          new Row(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              new Radio<_Location>(
                                value: _Location.Barbados,
                                groupValue: field.value,
                                onChanged: field.onChanged,
                              ),
268
                              const Text('Barbados')
Matt Perry's avatar
Matt Perry committed
269 270 271 272 273 274 275 276 277 278
                            ]
                          ),
                          new Row(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              new Radio<_Location>(
                                value: _Location.Bermuda,
                                groupValue: field.value,
                                onChanged: field.onChanged,
                              ),
279
                              const Text('Bermuda')
Matt Perry's avatar
Matt Perry committed
280 281 282 283 284 285 286 287 288
                            ]
                          )
                        ]
                      );
                    }
                  ),
                );
              }
            )
289 290 291 292
          );
        }
      ),
      new DemoItem<double>(
293
        name: 'Sun',
294
        value: 80.0,
295
        hint: 'Select sun level',
296
        valueToString: (double amount) => '${amount.round()}',
297
        builder: (DemoItem<double> item) {
298 299 300 301 302 303
          void close() {
            setState(() {
              item.isExpanded = false;
            });
          }

Matt Perry's avatar
Matt Perry committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
          return new Form(
            child: new Builder(
              builder: (BuildContext context) {
                return new CollapsibleBody(
                  onSave: () { Form.of(context).save(); close(); },
                  onCancel: () { Form.of(context).reset(); close(); },
                  child: new FormField<double>(
                    initialValue: item.value,
                    onSaved: (double value) { item.value = value; },
                    builder: (FormFieldState<double> field) {
                      return new Slider(
                        min: 0.0,
                        max: 100.0,
                        divisions: 5,
                        activeColor: Colors.orange[100 + (field.value * 5.0).round()],
                        label: '${field.value.round()}',
                        value: field.value,
                        onChanged: field.onChanged,
                      );
                    },
                  ),
                );
326
              }
Matt Perry's avatar
Matt Perry committed
327
            )
328 329 330 331 332 333 334 335 336
          );
        }
      )
    ];
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
337
      appBar: new AppBar(title: const Text('Expansion panels')),
338
      body: new SingleChildScrollView(
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        child: new Container(
          margin: const EdgeInsets.all(24.0),
          child: new ExpansionPanelList(
            expansionCallback: (int index, bool isExpanded) {
              setState(() {
                _demoItems[index].isExpanded = !isExpanded;
              });
            },
            children: _demoItems.map((DemoItem<dynamic> item) {
              return new ExpansionPanel(
                isExpanded: item.isExpanded,
                headerBuilder: item.headerBuilder,
                body: item.builder(item)
              );
            }).toList()
          )
        )
      )
    );
  }
}