Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to create a CupertinoAlertDialog

I'm trying to create an iOS CupertinoAlertDialog on my Flutter project using the following code:

   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"))
        ]));

However, when call this dialog I receive the following error message:

NoSuchMethodError: The getter 'alertDialogLabel' was called on null

The Android AlertDialog works properly.

What is wrong with this code?


Edit:

Solution: CupertinoAlertDialog crash

Just add GlobalCupertinoLocalizations.delegate to your MaterialApp.

like image 358
Notheros Avatar asked Oct 11 '18 16:10

Notheros


1 Answers

You create a method and you show the dialog from there

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),
      ),
    );
  }
}
like image 174
chemamolins Avatar answered Nov 23 '22 14:11

chemamolins