简体   繁体   中英

How to close the SimpleDialog in flutter after choosing an option

I added a SimpleDialog with 2 options: lost and found. Whenever I make my selection and get redirected to where I want, the SimpleDialog doesn't close and stays on my screen.

The switch:

switch (
  await showDialog(
    context: context,
    child: new SimpleDialog(
      title: new Text("Which category?"),
      children: <Widget>[
        new SimpleDialogOption(child: new Text("Found"),
          onPressed: () {
            goToCreate();
          },
        ),
        new SimpleDialogOption(child: new Text("Lost"),
          onPressed: () {
            //Whatever
          },
        ),
      ],
    )
  )
)

And the cases:

{
  case "Found":
    goToCreate();
    break;
  case "Lost":
    //Whatever
    break;
}

You can do this from the dialog when you press Accept (or whatever):

Navigator.pop(context, true); // You could return any Dart type, like an enum

From the caller:

bool dialogReturnValue = await showDialog(...);
if (dialogReturnValue == true){
    // do something
}

From official docs: https://docs.flutter.io/flutter/material/SimpleDialog-class.html

You need to execute inside options' onPressed method this:

Navigator.pop(context, ===arguments===);

Full example:

SimpleDialog(
        title: const Text('Select assignment'),
        children: <Widget>[
          SimpleDialogOption(
            onPressed: () { Navigator.pop(context, Department.treasury); },
            child: const Text('Treasury department'),
          ),
          SimpleDialogOption(
            onPressed: () { Navigator.pop(context, Department.state); },
            child: const Text('State department'),
          ),
        ],
      );

EDIT:

switch (
    await showDialog(
        context: context,
        child: new SimpleDialog(
          title: new Text("Which category?"),
          children: <Widget>[
            new SimpleDialogOption(child: new Text("Found"),
              onPressed: () {
                Navigator.pop(context, 'Found'); //Close the SimpleDialog then=>
                goToCreate();
              },
            ),
            new SimpleDialogOption(child: new Text("Lost"),
              onPressed: () {
                Navigator.pop(context, 'Lost'); //For closing the SimpleDialog
                //After that do whatever you want
              },
            ),
          ],
        )
    )
  )
)

EDIT 2 (Demo Application):

import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Test(),
    );
  }
}

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(onPressed: () {
          _askedToLead(context);
        }),
      ),
    );
  }

  Future<void> _askedToLead(BuildContext context) async {
    switch (await showDialog<String>(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: const Text('Select assignment'),
            children: <Widget>[
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, 'Found');
                },
                child: const Text('FOUND'),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, 'Lost');
                },
                child: const Text('LOST'),
              ),
            ],
          );
        })) {
      case 'Found':
        print('FOUND!');
        break;
      case 'Lost':
        print('LOST!');
        break;
    }
  }
}

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