简体   繁体   中英

Calling Stateful Class with simple alert dialog from another Class Flutter

I want to create a stateful class which has an alert dialog with content then how can i call it from another class. I am newbie in programming. Could you show with example code

Create a new dart file my_alerts.dart and define it as a library . You can then import the showSampleDialog function from it and use it in any class. Just pass the context from the class that is using it.

For more info about alerts see this article

 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(); }, ), ], ); }, ); }

Tomas answer is the way to go.

Here a running example: https://dartpad.dartlang.org/638db165becbe88b25bbd9c77f636297

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM