persistent_bottom_sheet_demo.dart 2.81 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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
class PersistentBottomSheetDemo extends StatefulWidget {
10
  static const String routeName = '/material/persistent-bottom-sheet';
11

12
  @override
13
  _PersistentBottomSheetDemoState createState() => _PersistentBottomSheetDemoState();
14 15 16
}

class _PersistentBottomSheetDemoState extends State<PersistentBottomSheetDemo> {
17
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
18

19 20 21 22 23
  VoidCallback _showBottomSheetCallback;

  @override
  void initState() {
    super.initState();
24
    _showBottomSheetCallback = _showBottomSheet;
25 26
  }

27
  void _showBottomSheet() {
28 29 30
    setState(() { // disable the button
      _showBottomSheetCallback = null;
    });
31
    _scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
32
      final ThemeData themeData = Theme.of(context);
33 34 35
      return Container(
        decoration: BoxDecoration(
          border: Border(top: BorderSide(color: themeData.disabledColor))
36
        ),
37
        child: Padding(
38
          padding: const EdgeInsets.all(32.0),
39
          child: Text('This is a Material persistent bottom sheet. Drag downwards to dismiss it.',
40
            textAlign: TextAlign.center,
41
            style: TextStyle(
42
              color: themeData.accentColor,
43 44 45 46
              fontSize: 24.0,
            ),
          ),
        ),
47
      );
48
    })
49 50 51 52 53 54
    .closed.whenComplete(() {
      if (mounted) {
        setState(() { // re-enable the button
          _showBottomSheetCallback = _showBottomSheet;
        });
      }
55 56 57
    });
  }

58
  void _showMessage() {
59
    showDialog<void>(
60
      context: context,
61
      builder: (BuildContext context) {
62
        return AlertDialog(
63 64
          content: const Text('You tapped the floating action button.'),
          actions: <Widget>[
65
            FlatButton(
66 67 68
              onPressed: () {
                Navigator.pop(context);
              },
69 70
              child: const Text('OK'),
            ),
71 72 73
          ],
        );
      },
74 75 76
    );
  }

77
  @override
78
  Widget build(BuildContext context) {
79
    return Scaffold(
80
      key: _scaffoldKey,
81 82 83 84 85 86
      appBar: AppBar(
        title: const Text('Persistent bottom sheet'),
        actions: <Widget>[
          MaterialDemoDocumentationButton(PersistentBottomSheetDemo.routeName),
        ],
      ),
87
      floatingActionButton: FloatingActionButton(
88
        onPressed: _showMessage,
89
        backgroundColor: Colors.redAccent,
90 91 92 93
        child: const Icon(
          Icons.add,
          semanticLabel: 'Add',
        ),
94
      ),
95 96
      body: Center(
        child: RaisedButton(
97
          onPressed: _showBottomSheetCallback,
98 99 100
          child: const Text('SHOW BOTTOM SHEET'),
        ),
      ),
101 102 103
    );
  }
}