📜  flutter showdialog barrierdismissible - Dart (1)

📅  最后修改于: 2023-12-03 15:00:47.974000             🧑  作者: Mango

Flutter Showdialog Barrierdismissible - Dart

In Flutter, we can use a ShowDialog widget to display a popup dialog on the screen. We can set the barrierDismissible property of the ShowDialog widget to control whether or not the user can dismiss the dialog by tapping outside of it.

Syntax
showDialog(
  context: context,
  barrierDismissible: true/false,
  // other dialog properties
);
  • context: The build context for the current widget tree.
  • barrierDismissible: A Boolean value that determines whether or not the dialog can be dismissed by tapping outside of it.
Usage
Displaying a Dialog with a Dismissible Barrier

To display a dialog with a dismissible barrier, we can set the barrierDismissible property of the ShowDialog widget to true. In this case, the user can dismiss the dialog by tapping outside of it.

showDialog(
  context: context,
  barrierDismissible: true,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text('Dismissable Dialog'),
      content: Text('Tap anywhere outside the dialog to dismiss.'),
      // other dialog properties
    );
  },
);
Displaying a Dialog with a Non-Dismissible Barrier

To display a dialog with a non-dismissible barrier, we can set the barrierDismissible property of the ShowDialog widget to false. In this case, the user cannot dismiss the dialog by tapping outside of it.

showDialog(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text('Non-Dismissable Dialog'),
      content: Text('This dialog cannot be dismissed by tapping outside.'),
      // other dialog properties
    );
  },
);
Conclusion

In conclusion, the barrierDismissible property of the ShowDialog widget allows us to control whether or not the user can dismiss a dialog by tapping outside of it. This feature can be useful in situations where we want to ensure that the user interacts with the dialog before continuing.