创建警报对话框

时间:2019-12-19 08:46:47

标签: flutter

我尝试打开警报,但出现错误,这是什么问题?

  

未处理的异常:找不到MaterialLocalizations。 MyApp小部件   要求MaterialLocalizations由Localizations小部件提供   祖先。

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  void _getCsvDocunent(BuildContext ctx) async {
    if (ctx != null) _showAlert(ctx, 'Hello world');
  }

  Future _showAlert(BuildContext context, String message) async {
    return showDialog(
        context: context,
        child: new AlertDialog(
          title: new Text(message),
          actions: <Widget>[
            new FlatButton(
                onPressed: () => Navigator.pop(context), child: new Text('Ok'))
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              FlatButton(
                child: Text('Show alert'),
                onPressed: () => _getCsvDocunent(
                    context), // show alert from _getCsvDocunent
              ),
            ],
          ),
        ),
      ),
    );
  }
}

4 个答案:

答案 0 :(得分:1)

FlatButton小部件包装Builder

Builder(
  builder: (context) {
    return FlatButton(
      child: Text('Show alert'),
      onPressed: () => _getCsvDocunent(context),
    );
  },
)

答案 1 :(得分:0)

在您的builder:小部件中包含showDialog

物料警报的示例代码

Future<void> _materialAlertInit(BuildContext context) async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return _materialAlert(context);
    },
  );
}

_materialAlert(BuildContext context) {
  return AlertDialog(
    title: Text('Material Alert'),
    content: Text('Android material designa alert'),
    actions: <Widget>[
      FlatButton(
        child: Text('Yes'),
        onPressed: () {
          Navigator.pop(context);
        },
      ),
      FlatButton(
        child: Text('No'),
        onPressed: () {
          Navigator.pop(context);
        },
      )
    ],
    elevation: 24.0,
    backgroundColor: Colors.white,
  );
}

答案 2 :(得分:0)

这是因为您在返回context时传递的showDialog无法找到MaterialLocalization小部件。错误提示MyApp小部件需要MaterialLocalization,这意味着我们需要在MaterialApp类中返回MyApp,而不是在您提供的build方法中返回。下面的示例工作代码:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Test",
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  State createState() => _HomeState();
}

class _HomeState extends State<Home> {
  void _getCsvDocunent(BuildContext ctx) async {
    if (ctx != null) _showAlert(ctx, 'Hello world');
  }

将按预期显示对话框:

enter image description here

答案 3 :(得分:0)

带有两个按钮的警告对话框

confirmationPopup(BuildContext context1) {
  showDialog(
      context: context1,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Alert"),
          content: Text("Are you sure you want to logout?"),
          actions: <Widget>[
            FlatButton(
              child: Text("No"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            FlatButton(
              child: Text("Yes"),
              onPressed: () {
                Navigator.of(context).pop();

              },
            )
          ],
        );
      });
}