在Flutter中显示警告对话框,无需使用onTap、onPressed等。

huangapple go评论49阅读模式
英文:

show alert dialog in flutter without onTap, onPressed etc

问题

如何在Flutter中无需用户交互显示警报对话框?

我有一个小部件,显示一个ListView。当特定条件为真时,我想在同一个小部件内显示一个警报对话框(在Flutter中一切都是小部件,对吗?)。因此,不需要用户交互。

这是我的小部件的构建方法:

@override
Widget build(BuildContext context, WidgetRef ref) {
  final providerData = ref.watch(someProvider);
  //showAlertDialog(context);
  final anotherProviderData = ref.watch(anotherProvider);

  return anotherProviderData.when(
    data: (entries) {
      return CustomScrollView(...

我想基于提供者数据显示一个AlertDialog

英文:

How do I display an alert dialog in flutter without user interaction?

I have a Widget that shows a ListView. When a specific condition is true, I want to display an alert dialog within the same widget (everything is a widget in Flutter, right?). So no user interaction is needed.

this is the build method of my widget:

@override
Widget build(BuildContext context, WidgetRef ref) {
final providerData = ref.watch(someProvider);
//showAlertDialog(context);
final anotherProviderData = ref.watch(anotherProvider);

return anotherProviderData.when(
  data: (entries) {
    return CustomScrollView(...

I want to display an AlertDialog based upon the provider data.

答案1

得分: 1

你可以从根据你的触发调用的函数中调用 showDialog

showDialog(
    context: context,
    builder: (BuildContext context) {
      return MyAlertDialog();
});
英文:

You can call showDialog from a function you call based upon your trigger.

showDialog(
    context: context,
    builder: (BuildContext context) {
      return MyAlertDialog();
});

答案2

得分: 0

我弄清楚如何做了:

Future(() {
    showAlertDialog(context);
});

showAlertDialog(context) 是显示警报对话框的方法。

英文:

I figured out how to do it:

Future(() {
        showAlertDialog(context);
      });

showAlertDialog(context) is the method showing the alert dialog.

huangapple
  • 本文由 发表于 2023年2月23日 21:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定