cupertino_alert_dialog.0.dart 2.18 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flutter code sample for CupertinoAlertDialog

import 'package:flutter/cupertino.dart';

9
void main() => runApp(const AlertDialogApp());
10

11 12
class AlertDialogApp extends StatelessWidget {
  const AlertDialogApp({Key? key}) : super(key: key);
13 14 15 16

  @override
  Widget build(BuildContext context) {
    return const CupertinoApp(
17 18
      theme: CupertinoThemeData(brightness: Brightness.light),
      home: AlertDialogExample(),
19 20 21 22
    );
  }
}

23 24
class AlertDialogExample extends StatelessWidget {
  const AlertDialogExample({Key? key}) : super(key: key);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  // This shows a CupertinoModalPopup which hosts a CupertinoAlertDialog.
  void _showAlertDialog(BuildContext context) {
    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: const Text('Alert'),
        content: const Text('Proceed with destructive action?'),
        actions: <CupertinoDialogAction>[
          CupertinoDialogAction(
            /// This parameter indicates this action is the default,
            /// and turns the action's text to bold text.
            isDefaultAction: true,
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('No'),
          ),
          CupertinoDialogAction(
            /// This parameter indicates the action would perform
            /// a destructive action such as deletion, and turns
            /// the action's text color to red.
            isDestructiveAction: true,
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Yes'),
52
          ),
53 54 55 56 57 58 59 60
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
61 62
      navigationBar: const CupertinoNavigationBar(
        middle: Text('CupertinoAlertDialog Sample'),
63 64 65 66 67 68 69 70 71 72
      ),
      child: Center(
        child: CupertinoButton(
          onPressed: () => _showAlertDialog(context),
          child: const Text('CupertinoAlertDialog'),
        ),
      ),
    );
  }
}