选择选项后如何在抖动中关闭SimpleDialog

时间:2019-05-16 12:50:27

标签: android flutter simpledialog

我添加了一个带有2个选项的SimpleDialog,丢失并找到了。每当我做出选择并重定向到我想要的位置时,the SimpleDialog doesn't close and stays on my screen.

开关: 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 }, ), ], ) ) ) )

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

2 个答案:

答案 0 :(得分:1)

您可以在按“接受”(或其他)时从对话框中执行以下操作:

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

来自呼叫者:

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

答案 1 :(得分:0)

摘自官方文档:https://docs.flutter.io/flutter/material/SimpleDialog-class.html

您需要在options内部执行onPressed方法:

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

完整示例:

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

编辑:

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

编辑2(演示应用程序):

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;
    }
  }
}