Создайте новый файл дротика my_alerts.dart
и определите его как library
. Затем вы можете импортировать из него функцию showSampleDialog
и использовать ее в любом классе. Просто передайте context
от класса, который его использует.
Для получения дополнительной информации об оповещениях см. Эту статью
library my_alerts;
import 'package:flutter/material.dart';
// user defined function
void showSampleDialog(BuildContext context) {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}