Flutter - 如何创建 CupertinoAlertDialog

Not*_*ros 7 ios android-alertdialog flutter

我正在尝试CupertinoAlertDialog使用以下代码在我的 Flutter 项目上创建一个 iOS :

   showDialog(
      context: context,
      builder: (BuildContext context) => new CupertinoAlertDialog(
        title: new Text("Alert"),
        content: new Text("My alert message"),
        actions: [
          CupertinoDialogAction(isDefaultAction: true, child: new Text("Close"))
        ]));
Run Code Online (Sandbox Code Playgroud)

但是,当调用此对话框时,我收到以下错误消息:

NoSuchMethodError: The getter 'alertDialogLabel' was called on null

安卓AlertDialog运行正常。

这段代码有什么问题?


编辑:

解决方案:CupertinoAlertDialog 崩溃

只需添加GlobalCupertinoLocalizations.delegate到您的 MaterialApp。

che*_*ins 7

您创建一个方法并从那里显示对话框

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void displayDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) => new CupertinoAlertDialog(
            title: new Text("Alert"),
            content: new Text("My alert message"),
            actions: [
              CupertinoDialogAction(
                  isDefaultAction: true, child: new Text("Close"))
            ],
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(child: new Text("Welcome")),
      floatingActionButton: new FloatingActionButton(
        onPressed: displayDialog,
        child: new Icon(Icons.add),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)