1

I want to Call a Dialog Function from another Class File in flutter what is the Proper Way of doing That...

File1:-

 import 'package:eg/eg/dialog_file.dart'

    floatingActionButton: FloatingActionButton(
            onPressed: () {
            // This is where I want to call the function
            },
            backgroundColor: Colors.black,
            child: Container(
              child: Icon(
                icons.add,
                size: 23,
                color: Colors.white,
              ),
            ),
          ),

 

File2:-

    showMyDialog(parentContext) {
    return showDialog(
      context: parentContext,
      builder: (context) {
        return SimpleDialog(
          title: Text("Title"),
          children: <Widget>[
            SimpleDialogOption(
              child: Text("Option1"),
              onPressed: function1,
            ),
            SimpleDialogOption(
              child: Text("Option2"),
              onPressed: function2,
            ),
            SimpleDialogOption(
              child: Text("Option3"),
              onPressed: function3,
            ),
          ],
        );
      },
    );
  }

So the Dialog Function have onPressed Functions that are in the File2 so I am unable to write a custom dialog Function in File1

how could I achieve this?

3
  • You should provide some more context so I can be of better help. However, if you make the 'showMyDialog' method static, then 'File2.showMyDialog();' You can run the dialog by doing
    – Salih Can
    Jul 24, 2021 at 4:11
  • @SalihCan it does not work 'function1', 'function2' and 'function3' gives the error "Instance members can't be accessed from a static method. Try removing the reference to the instance member, or removing the keyword 'static' from the method." Jul 24, 2021 at 4:30
  • The code you provided doesn't quite do what you want to do, so I can only offer limited help. According to the error you should make function1,2,3 static.
    – Salih Can
    Jul 24, 2021 at 4:37

2 Answers 2

2

You don't mention where your function1 etc... come from but in general here is one way call a dialog from another class. I make them static so I don't have to create an instance of the class.

class Dialogs {
  static Future<void> showMyDialog(parentContext) {
    return showDialog(
      context: parentContext,
      builder: (context) {
        return SimpleDialog(
          title: Text("Title"),
          children: <Widget>[
            SimpleDialogOption(child: Text("Option1"), onPressed: () {}),
            SimpleDialogOption(child: Text("Option2"), onPressed: () {}),
            SimpleDialogOption(child: Text("Option3"), onPressed: () {})
          ],
        );
      },
    );
  }
}

And here's an example of a button that will display the above dialog by passing in the context from the build method that it is inside of.

 ElevatedButton(
    onPressed: () => Dialogs.showMyDialog(context),
     child: Text('Dialog'),
 ),
-1

You should try below answer :

First write your Dialog Function in other class

 @override
 Widget build(BuildContext context) {
 return Container(
    color: Colors.white30,
    child:AlertDialog(
    title: Text(
       'name',
       style: TextStyle(
       fontWeight: FontWeight.bold,
      ),
      textAlign: TextAlign.center,
    ),
    content: Text(
      'name',
       style: TextStyle(
       fontWeight: FontWeight.bold,
      ),
      textAlign: TextAlign.center,
    ),
    actions: [
       TextButton(
        onPressed: () => Navigator.of(context).pop(),
        child: Text('OK'),
       )
     ],
   ),
  );
}

Then write dialog class function in other class or where you call it like below:

onPressed: () {
   Navigator.push(
   context,
   MaterialPageRoute(
     builder: (context) => CampaignData(),
    ),
   );
  },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.