使用常规对话框颤动创建时,警告对话框按钮不起作用



在此处输入图像描述

我创建了一个通用对话框,可以创建返回一些值的按钮

import 'package:flutter/material.dart';
typedef DialogOptionBuilder<T> = Map<String, T?> Function();
Future<T?> showGenericDialog<T>({
required BuildContext context,
required String title,
required String content,
required DialogOptionBuilder optionBuilder,
}) {
final options = optionBuilder();
return showDialog<T>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(title),
content: Text(content),
actions: options.keys.map((optionTitle) {
final value = options[optionTitle];
return TextButton(
onPressed: () {
if (value) {
Navigator.of(context).pop(value);
} else {
Navigator.of(context).pop();
}
},
child: Text(optionTitle),
);
}).toList(),
);
},
);
}

当我试图使用这个通用对话框来创建一个简单的警报对话框时;ok";这个";ok";按钮没有弹出警报对话框,即使我已经编码为在按下按钮时对话框没有返回值时弹出对话框


Future<void> showCannotShareEmptyNoteDialog(BuildContext context) async {
return showGenericDialog<void>(
context: context,
title: 'Sharing',
content: "You can't share a empty note",
optionBuilder: () => {
'OK': null,
},
);
}

在这里,我调用警报对话框

appBar: AppBar(
title: const Text("New Note"),
actions: [
IconButton(
onPressed: () async {
final text = _textController.text;
if (_note == null || text.isEmpty) {
await showCannotShareEmptyNoteDialog(context);
} else {
Share.share(text);
}
},
icon: const Icon(Icons.share),
),
],
),

ok按钮没有从屏幕中删除警报对话框

这是因为您下面的条件抛出异常。由于您的类型是void,因此不返回任何值。

...
return TextButton(
onPressed: () {
if (value) {  // this one is caused the error. because its null. not bool
Navigator.of(context).pop(value);
} else {
Navigator.of(context).pop();
}

错误:

════════ Exception caught by gesture ═════════
type 'Null' is not a subtype of type 'bool'

更改为:

return TextButton(
onPressed: () {
if (value == true) {
Navigator.of(context).pop(value);
} else {
print(value);
Navigator.of(context).pop();
}
},

最新更新