簡體   English   中英

Flutter 等待對話結果

[英]Flutter awaiting result of dialog

我有以下代碼,通過單擊 FlatButton 調用:

_performOrderCheck(BuildContext context) async {
    bool _checksCompleted = await _performBundleCheck(context);
    print("Sepp");
    print(_checksCompleted);

    if (_checksCompleted) {
      _addArticleToOrder(_innerQty, _article);
      Navigator.pop(context);
    }
  }

Future<bool> _performBundleCheck(BuildContext context) async {
    //check bundles
    if (!_article.checkBundeledArticles()) {
      showDialog(
          context: context,
          builder: (_) => AlertDialog(
                title: Text('Menü unvollständig'),
                content: Text(
                    'Sie haben nicht alle möglichen Artikel gewählt. Wollen sie dennoch fortfahren?'),
                actions: <Widget>[
                  FlatButton(
                      onPressed: () {
                        Navigator.pop(_);
                        return false;
                      },
                      child: Text('Nein')),
                  FlatButton(
                      onPressed: () {
                        //_addArticleToOrder(_innerQty, _article);
                        Navigator.pop(_);
                        return true;
                        //Navigator.pop(context);
                      },
                      child: Text('Ja')),
                ],
                elevation: 24,
              ),
          barrierDismissible: false);
    } else {
      return true;
    }
  }

我想要的是可以等待用戶決定,然后調用“_addArticleToOrder”。 那可能嗎?

謝謝你的幫助。

您可以在 showdialog 前面添加 await 關鍵字,並在 show dialog 結束時返回值。

添加等待。

  await showDialog(

添加返回值

 barrierDismissible: false);
      return true; // added line

當接受的答案有效時,結果總是返回true

如果您想獲得對話框的結果,單擊“Nein”可能為,單擊“Ja”可能為,代碼如下:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          _performOrderCheck(context);
        },
        // onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  _performOrderCheck(BuildContext context) async {
    _incrementCounter();

    print("_performOrderCheck called");
    bool _checksCompleted = await _performBundleCheck(context);
    print("_checksCompleted result: $_checksCompleted");

    if (_checksCompleted) {
      print("_checksCompleted");
    }
  }

  Future<bool> _performBundleCheck(BuildContext context) async {
    //check bundles
    if (true) {
      return await showDialog(
        context: context,
        builder: (_) => AlertDialog(
          title: Text('Menü unvollständig'),
          content: Text(
              'Sie haben nicht alle möglichen Artikel gewählt. Wollen sie dennoch fortfahren?'),
          actions: <Widget>[
            FlatButton(
                onPressed: () {
                  Navigator.pop(context, false);
                },
                child: Text('Nein')),
            FlatButton(
                onPressed: () {
                  Navigator.pop(context, true);
                },
                child: Text('Ja')),
          ],
          elevation: 24,
        ),
        barrierDismissible: false,
      );
    } else {
      return true;
    }
  }
}

使用Navigator.pop(context, false); Navigator.pop(context, true); 將對話框的結果返回給showDialog 使用return await將其從_performBundleCheck function 返回到_performOrderCheck

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM