• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7import '../../gallery/demo.dart';
8
9class PersistentBottomSheetDemo extends StatefulWidget {
10  static const String routeName = '/material/persistent-bottom-sheet';
11
12  @override
13  _PersistentBottomSheetDemoState createState() => _PersistentBottomSheetDemoState();
14}
15
16class _PersistentBottomSheetDemoState extends State<PersistentBottomSheetDemo> {
17  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
18
19  VoidCallback _showBottomSheetCallback;
20
21  @override
22  void initState() {
23    super.initState();
24    _showBottomSheetCallback = _showBottomSheet;
25  }
26
27  void _showBottomSheet() {
28    setState(() { // disable the button
29      _showBottomSheetCallback = null;
30    });
31    _scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
32      final ThemeData themeData = Theme.of(context);
33      return Container(
34        decoration: BoxDecoration(
35          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              fontSize: 24.0,
44            ),
45          ),
46        ),
47      );
48    })
49    .closed.whenComplete(() {
50      if (mounted) {
51        setState(() { // re-enable the button
52          _showBottomSheetCallback = _showBottomSheet;
53        });
54      }
55    });
56  }
57
58  void _showMessage() {
59    showDialog<void>(
60      context: context,
61      builder: (BuildContext context) {
62        return AlertDialog(
63          content: const Text('You tapped the floating action button.'),
64          actions: <Widget>[
65            FlatButton(
66              onPressed: () {
67                Navigator.pop(context);
68              },
69              child: const Text('OK'),
70            ),
71          ],
72        );
73      },
74    );
75  }
76
77  @override
78  Widget build(BuildContext context) {
79    return Scaffold(
80      key: _scaffoldKey,
81      appBar: AppBar(
82        title: const Text('Persistent bottom sheet'),
83        actions: <Widget>[
84          MaterialDemoDocumentationButton(PersistentBottomSheetDemo.routeName),
85        ],
86      ),
87      floatingActionButton: FloatingActionButton(
88        onPressed: _showMessage,
89        backgroundColor: Colors.redAccent,
90        child: const Icon(
91          Icons.add,
92          semanticLabel: 'Add',
93        ),
94      ),
95      body: Center(
96        child: RaisedButton(
97          onPressed: _showBottomSheetCallback,
98          child: const Text('SHOW BOTTOM SHEET'),
99        ),
100      ),
101    );
102  }
103}
104