【问题标题】:How to close the SimpleDialog in flutter after choosing an option选择选项后如何关闭SimpleDialog
【发布时间】:2019-05-16 12:50:27
【问题描述】:

我添加了一个 SimpleDialog 有 2 个选项:失物招领处。每当我做出选择并被重定向到我想要的位置时,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;
}

【问题讨论】:

  • 您能否添加一些代码来支持您的问题。
  • @TinusJackson 我刚刚做了

标签: android flutter simpledialog


【解决方案1】:

来自官方文档: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;
    }
  }
}

【讨论】:

  • 你能检查我上面的代码吗?我只是尝试应用这个方法,但没有奏效,也许你能找到问题?
  • 我更改了代码还为您的案例创建了一个演示应用程序。我希望它有所帮助。
【解决方案2】:

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

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

来自调用者:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多