snack_bar_demo.dart 2.82 KB
Newer Older
1 2 3 4 5 6
// 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';

7 8
import '../../gallery/demo.dart';

9
const String _text1 =
10 11 12
  'Snackbars provide lightweight feedback about an operation by '
  'showing a brief message at the bottom of the screen. Snackbars '
  'can contain an action.';
13 14

const String _text2 =
15 16
  'Snackbars should contain a single line of text directly related '
  'to the operation performed. They cannot contain icons.';
17 18

const String _text3 =
19
  'By default snackbars automatically disappear after a few seconds ';
20

21
class SnackBarDemo extends StatefulWidget {
22
  const SnackBarDemo({ Key key }) : super(key: key);
23

24
  static const String routeName = '/material/snack-bar';
25

26
  @override
27
  _SnackBarDemoState createState() => _SnackBarDemoState();
28 29 30 31 32
}

class _SnackBarDemoState extends State<SnackBarDemo> {
  int _snackBarIndex = 1;

33
  Widget buildBody(BuildContext context) {
34
    return SafeArea(
35 36
      top: false,
      bottom: false,
37
      child: ListView(
38 39 40 41
        padding: const EdgeInsets.all(24.0),
        children: <Widget>[
          const Text(_text1),
          const Text(_text2),
42 43
          Center(
            child: RaisedButton(
44 45 46
              child: const Text('SHOW A SNACKBAR'),
              onPressed: () {
                final int thisSnackBarIndex = _snackBarIndex++;
47 48 49
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text('This is snackbar #$thisSnackBarIndex.'),
                  action: SnackBarAction(
50 51
                    label: 'ACTION',
                    onPressed: () {
52
                      Scaffold.of(context).showSnackBar(SnackBar(
53
                        content: Text('You pressed snackbar $thisSnackBarIndex\'s action.'),
54
                      ));
55
                    },
56 57
                  ),
                ));
58
              },
59 60 61 62
            ),
          ),
          const Text(_text3),
        ]
63
        .map<Widget>((Widget child) {
64
          return Container(
65
            margin: const EdgeInsets.symmetric(vertical: 12.0),
66
            child: child,
67 68
          );
        })
69
        .toList(),
70
      ),
71 72 73
    );
  }

74
  @override
75
  Widget build(BuildContext context) {
76 77
    return Scaffold(
      appBar: AppBar(
78 79
        title: const Text('Snackbar'),
        actions: <Widget>[MaterialDemoDocumentationButton(SnackBarDemo.routeName)],
80
      ),
81
      body: Builder(
82 83 84
        // Create an inner BuildContext so that the snackBar onPressed methods
        // can refer to the Scaffold with Scaffold.of().
        builder: buildBody
85
      ),
86 87 88 89 90
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add),
        tooltip: 'Create',
        onPressed: () {
          print('Floating Action Button was pressed');
91
        },
92
      ),
93 94 95
    );
  }
}