cupertino_dialog_demo.dart 6.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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/cupertino.dart';
import 'package:flutter/material.dart';

class CupertinoDialogDemo extends StatefulWidget {
  static const String routeName = '/cupertino/dialog';

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

class _CupertinoDialogDemoState extends State<CupertinoDialogDemo> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

18
  void showDemoDialog<T>({BuildContext context, Widget child}) {
19 20
    showDialog<T>(
      context: context,
21
      barrierDismissible: false,
22
      builder: (BuildContext context) => child,
23 24
    ).then<void>((T value) {
      // The value passed to Navigator.pop() or null.
25
      if (value != null) {
26 27 28 29 30
        _scaffoldKey.currentState.showSnackBar(
          new SnackBar(
            content: new Text('You selected: $value'),
          ),
        );
31 32 33 34 35 36 37 38 39
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      appBar: new AppBar(
40
        title: const Text('Cupertino Dialogs'),
41 42 43
      ),
      body: new ListView(
        padding: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 72.0),
44
        children: <Widget>[
45
          new CupertinoButton(
46
            child: const Text('Alert'),
47
            color: CupertinoColors.activeBlue,
48 49 50 51
            onPressed: () {
              showDemoDialog<String>(
                context: context,
                child: new CupertinoAlertDialog(
52
                  title: const Text('Discard draft?'),
53 54
                  actions: <Widget>[
                    new CupertinoDialogAction(
55
                      child: const Text('Discard'),
56
                      isDestructiveAction: true,
57 58 59
                      onPressed: () {
                        Navigator.pop(context, 'Discard');
                      },
60 61
                    ),
                    new CupertinoDialogAction(
62 63
                      child: const Text('Cancel'),
                      isDefaultAction: true,
64 65 66
                      onPressed: () {
                        Navigator.pop(context, 'Cancel');
                      },
67
                    ),
68
                  ],
69 70 71 72
                ),
              );
            },
          ),
73
          const Padding(padding: const EdgeInsets.all(8.0)),
74
          new CupertinoButton(
75
            child: const Text('Alert with Title'),
76
            color: CupertinoColors.activeBlue,
77
            padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 36.0),
78 79 80 81
            onPressed: () {
              showDemoDialog<String>(
                context: context,
                child: new CupertinoAlertDialog(
82
                  title: const Text('Allow "Maps" to access your location while you are using the app?'),
83 84
                  content: const Text('Your current location will be displayed on the map and used '
                      'for directions, nearby search results, and estimated travel times.'),
85 86
                  actions: <Widget>[
                    new CupertinoDialogAction(
87
                      child: const Text('Don\'t Allow'),
88 89 90
                      onPressed: () {
                        Navigator.pop(context, 'Disallow');
                      },
91 92
                    ),
                    new CupertinoDialogAction(
93
                      child: const Text('Allow'),
94 95 96
                      onPressed: () {
                        Navigator.pop(context, 'Allow');
                      },
97
                    ),
98
                  ],
99 100 101 102
                ),
              );
            },
          ),
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
          const Padding(padding: const EdgeInsets.all(8.0)),
          new CupertinoButton(
            child: const Text('Alert with Buttons'),
            color: CupertinoColors.activeBlue,
            padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 36.0),
            onPressed: () {
              showDemoDialog<String>(
                context: context,
                child: const CupertinoDessertDialog(
                  title: const Text('Select Favorite Dessert'),
                  content: const Text('Please select your favorite type of dessert from the '
                      'list below. Your selection will be used to customize the suggested '
                      'list of eateries in your area.'),
                ),
              );
            },
          ),
          const Padding(padding: const EdgeInsets.all(8.0)),
          new CupertinoButton(
            child: const Text('Alert Buttons Only'),
            color: CupertinoColors.activeBlue,
            padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 36.0),
            onPressed: () {
              showDemoDialog<String>(
                context: context,
                child: const CupertinoDessertDialog(),
              );
            },
          ),
132 133 134 135 136
        ],
      ),
    );
  }
}
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

class CupertinoDessertDialog extends StatelessWidget {
  const CupertinoDessertDialog({Key key, this.title, this.content}) : super(key: key);

  final Widget title;
  final Widget content;

  @override
  Widget build(BuildContext context) {
    return new CupertinoAlertDialog(
      title: title,
      content: content,
      actions: <Widget>[
        new CupertinoDialogAction(
          child: const Text('Cheesecake'),
          onPressed: () {
            Navigator.pop(context, 'Cheesecake');
          },
        ),
        new CupertinoDialogAction(
          child: const Text('Tiramisu'),
          onPressed: () {
            Navigator.pop(context, 'Tiramisu');
          },
        ),
        new CupertinoDialogAction(
          child: const Text('Apple Pie'),
          onPressed: () {
            Navigator.pop(context, 'Apple Pie');
          },
        ),
        new CupertinoDialogAction(
          child: const Text("Devil's food cake"),
          onPressed: () {
            Navigator.pop(context, "Devil's food cake");
          },
        ),
        new CupertinoDialogAction(
          child: const Text('Banana Split'),
          onPressed: () {
            Navigator.pop(context, 'Banana Split');
          },
        ),
        new CupertinoDialogAction(
          child: const Text('Oatmeal Cookie'),
          onPressed: () {
            Navigator.pop(context, 'Oatmeal Cookies');
          },
        ),
        new CupertinoDialogAction(
          child: const Text('Chocolate Brownie'),
          onPressed: () {
            Navigator.pop(context, 'Chocolate Brownies');
          },
        ),
        new CupertinoDialogAction(
          child: const Text('Cancel'),
          isDestructiveAction: true,
          onPressed: () {
            Navigator.pop(context, 'Cancel');
          },
        ),
      ],
    );
  }
}