如何让Flutter对话框消失?

3

我一直在尝试解决如何让我的Flutter对话框消失的问题。我已经尝试了所有方法,但是在方法完成处理之前,它仍然不会消失。

背景:制作一个应用程序,允许用户从其设备中选择图像,然后将其与用户定义的名称(在AlertDialog中接收,但不会立即消失)一起压缩。

我尝试过以下方法来立即使AlertDialog消失:

  1. Navigator.of(context).pop()
  2. Navigator.pop(context)
  3. Navigator.of(context, rootNavigator: true).pop()
  4. 尝试确保调用它之后异步执行该方法,以便不会阻塞

在pubspec.yaml中添加导入

path_provider: ^2.0.2
archive: ^3.1.2
file_picker: ^4.0.3

以下是代码:

import 'dart:io';

import 'package:archive/archive_io.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class HomePage extends StatefulWidget {
  final String title;

  const HomePage({required this.title, Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  late final Directory _uploadsDir;

  @override
  void initState() {
    super.initState();
    _init();
  }

  @override
  void dispose() async {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => _importFilesSelected(context),
          child: const Text("Select Images"),
        ),
      ),
    );
  }

  void _init() async {
    Directory dir = await getApplicationDocumentsDirectory();
    _uploadsDir = await Directory(join(dir.path, "uploads")).create(recursive: true);
  }

  void _importFilesSelected(BuildContext context) async {
    final BuildContext buildContext = context;

    FilePickerResult? result = await FilePicker.platform.pickFiles(
        dialogTitle: "Select File(s) to Import",
        allowMultiple: true,
        type: FileType.custom,
        allowedExtensions: ['jpg', 'png'],
        onFileLoading: (FilePickerStatus status) {
          if (status == FilePickerStatus.picking) {
            showDialog(
              context: buildContext,
              barrierDismissible: false,
              builder: (BuildContext context) {
                return Dialog(
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: const [
                        CircularProgressIndicator(),
                        SizedBox(width: 10.0,),
                        Text("Processing Files..."),
                      ],
                    ),
                  ),
                );
              },
            );
          } else {
            Navigator.pop(buildContext);
          }
        },
        withData: false
    );

    _processResult(result, buildContext);
  }

  void _processResult(FilePickerResult? result, BuildContext buildContext) async {
    if (result != null) {
      String? zipFilename = await _showGetZipFileNameDialog(buildContext);
      print("zipFileName: $zipFilename");
      _processImagesIntoZip(result, zipFilename!);
    }
  }

  void _processImagesIntoZip(FilePickerResult result, String zipFilename) async {
    String zipPath = join(_uploadsDir.path, zipFilename);

    final encoder = ZipFileEncoder();
    encoder.create(zipPath);

    for (PlatformFile file in result.files) {
      encoder.addFile(File(file.path!));
    }
  }

  Future<String?> _showGetZipFileNameDialog(BuildContext context) {
    String? zipFilename;

    return showDialog<String>(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return AlertDialog(
            title: const Text("Save As"),
            content: Form(
              key: _formKey,
              child: TextFormField(
                decoration: const InputDecoration(
                  labelText: "Save As:",
                ),
                validator: (val) {
                  return val!.isEmpty ? "Please fill out filename" : null;
                },
                onSaved: (val) => zipFilename = "${val!}.zip",
              ),
            ),
            actions: [
              TextButton(
                child: const Text("Cancel"),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              TextButton(
                child: const Text("OK"),
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                    _formKey.currentState!.save();

                    // Remove the Dialog from the screen
                    Navigator.pop(context, zipFilename);
                  }
                },
              ),
            ],
          );
        }
    );
  }
}


嗨,请查看以下链接:https://dev59.com/_FUK5IYBdhLWcg3wsBv6#62221142 - Jessé Lopes Pereira
2个回答

2
我有类似的问题,使用以下方法解决:

被修复了

Navigator.pop(context);

我感谢你的回答,但我已经尝试过了。我编辑了我的问题以使其更清晰。 - user1234567890

1

Navigator.pop(context) 是正确的答案,如果它不起作用,那么就意味着有些东西出了问题。

你创建了两个导航器吗?例如,当您意外创建新的 MaterialApp 而不是 Scaffold 时,就会发生这种情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接