Navigator.of(context).pop()给我黑屏

时间:2020-01-17 05:12:46

标签: flutter dart

Noob在这里。 我做了一个带有颤动的更新检查器,但是如果我选择任何按钮,它就会给我黑屏。 我怎样才能解决这个问题?有什么想法吗?

屏幕

When I press button Entire screen change to black

代码

import 'package:LiteCalculator/updater/bean/UpdaterBean.dart';
import 'package:flutter/material.dart';

class UpdateHolder extends StatelessWidget {
  final List<Version> version;

  UpdateHolder({Key key, this.version}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return calculateResult(
        version[0].latestVersion, version[1].currentVersion, context);
  }

  Widget calculateResult(latestVersion, currentVersion, context) {
    print('Latest Version : ${int.parse(latestVersion)}');
    print('Current Version : ${int.parse(currentVersion)}');
    Widget data;
    if ((int.parse(currentVersion) <= int.parse(latestVersion))) {
      data = Center(
        child: createAlert('Update Required', actions: <Widget>[
          FlatButton(
            child: Text('OK'),
            onPressed: () {
              print('OK Button Pressed.');
              Navigator.of(context).pop();
            },
          ),
          FlatButton(
            child: Text('Later'),
            onPressed: () {
              print('Later Button Pressed.');
              Navigator.of(context).pop();
            },
          ),
        ]),
      );
    } else
      data = Center();
    return data;
  }

  Widget createAlert(content, {List<Widget> actions, title}) {
    AlertDialog snackBar;
    snackBar = AlertDialog(
      content: Text(content),
      actions: actions,
    );
    return snackBar;
  }
}

5 个答案:

答案 0 :(得分:1)

为您的弹出窗口调用

void showDialogPopup(){
      showDialog(
        context: context,
        builder: (_)=>AlertDialog(
          backgroundColor: Colors.transparent,
          content: Container(
          child: Center(
             child: FlatButton(
               onPressed: (){
                 Navigator.of(context).pop(null);
               },
               child: Center(
                 child: Text("close")
               )
             )
           )
          )
        )
      );
    }

答案 1 :(得分:0)

黑屏还是黑屏?如果屏幕黑屏,则说明您没有用MaterialApp包装主窗口小部件(在runApp中使用)。

答案 2 :(得分:0)

您可以参考这个。

我用url_launcher 5.4.1打开PlayStore网站。

import "package:flutter/material.dart";
import 'package:url_launcher/url_launcher.dart';

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

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstPage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  @override
  void initState() {
    _checkUpdate();
    super.initState();
  }

  Future<void> _checkUpdate() async {
    await Future.delayed(Duration.zero);
    await showDialog(
      context: context,
      builder: (context) => UpdateDialog(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("First Page"),
      ),
    );
  }
}

class UpdateDialog extends StatefulWidget {
  @override
  _UpdateDialogState createState() => _UpdateDialogState();
}

class _UpdateDialogState extends State<UpdateDialog> {
  Future<void> _updateFound;

  @override
  void initState() {
    _updateFound = _checkForUpdate();
    super.initState();
  }

  Future<bool> _checkForUpdate() async {
    await Future.delayed(Duration.zero);
    bool updateFound = false;
    await Future.delayed(Duration(seconds: 3)); // Do Get call to server
    updateFound = true;
    if (!updateFound) Navigator.pop(context);
    return updateFound;
  }

  Future<void> _openWebPage() async {
    Navigator.pop(context);
    launch("https://play.google.com"); //Your link `url_launcher` package
  }

  void _laterClicked(){
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _updateFound,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting)
          return AlertDialog(
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                CircularProgressIndicator(),
                const SizedBox(height: 12.0),
                Text("Checking for Update"),
              ],
            ),
          );
        else if (snapshot.hasError)
          return AlertDialog(
            title: Text("Error Occured"),
            content: Text("ERROR: ${snapshot.error}"),
          );
        else if(snapshot.data)
          return AlertDialog(
            title: Text("Update Required"),
            content: Text(
                "Latest version found. Need an update. bla bla bla bla bla bla"),
            actions: <Widget>[
              FlatButton(
                child: Text("OK"),
                onPressed: _openWebPage,
              ),
              FlatButton(
                child: Text("LATER"),
                onPressed: _laterClicked,
              ),
            ],
          );
        else
          return const SizedBox();
      },
    );
  }
}

答案 3 :(得分:0)

只要您尝试使用小部件的pop context,就会发生这种情况。

在以下代码中:

FlatButton(
  child: Text('OK'),
   onPressed: () {
     print('OK Button Pressed.');
     Navigator.of(context).pop();
   },
)

context表示窗口小部件的上下文本身(在build方法中提供)。

要解决此问题,而不是创建一个Dialog小部件并将其作为主要小部件返回,只需使用showDialog并返回一个简单的Container()。

使用dialogContext弹出对话框,而不是窗口小部件本身。

例如:

 if ((int.parse(currentVersion) <= int.parse(latestVersion))) {
     showDialog(
       builder: (dialogContext) => AlertDialog(
        content: Text('Update Required'),
       actions: <Widget>[
         FlatButton(
            child: Text('OK'),
            onPressed: () {
              print('OK Button Pressed.');
              Navigator.of(dialogContext).pop();
            },
          ),
          FlatButton(
            child: Text('Later'),
            onPressed: () {
              print('Later Button Pressed.');
              Navigator.of(dialogContext).pop();
            },
          ),
        ],
     ),
    );
 }
return Container();

答案 4 :(得分:0)

我有一个类似的问题,我的解决方法是这样的:

bool hasBeenShown = false;
if(!hasBeenShown) {
    Navigator.pop(context);
}
hasBeenShown = true;

对我来说,问题是由于某种原因,Navigator.pop应该只被调用一次而被多次调用。