[android] FlatButton 클릭시 AlertDialog를 닫는 방법은 무엇입니까?

다음이 AlertDialog있습니다.

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text("Location disabled"),
              content: const Text(
                  """
Location is disabled on this device. Please enable it and try again.
                  """),
              actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: _dismissDialog,
                ),
              ],
            ),
        );

_dismissDialog()기각을 어떻게 할 수 AlertDialog있습니까?



답변

Navigator.pop()트릭을해야합니다. 이를 사용하여 대화 상자의 결과를 반환 할 수도 있습니다 (사용자에게 선택 사항을 제공 한 경우).


답변

Navigator.of(context, rootNavigator: true).pop('dialog')

나와 함께 일했습니다.


답변

Navigator.pop(_)

나를 위해 일했지만 Flutter Team의 갤러리에는 다음을 사용하는 예제가 포함되어 있습니다.

Navigator.of(context, rootNavigator: true).pop()

그것도 효과가 있고 나는 그들의지도를 따르고 싶은 유혹을 받는다.


답변

결과를 반환하지 않으려면 다음 중 하나를 사용하십시오.

Navigator.of(context).pop();
Navigator.pop(context);

그러나 결과를 반환하려면 다음을 참조하십시오.

예:

showDialog(
    context: context,
    builder: (_) {
      return AlertDialog(
        title: Text('Wanna Exit?'),
        actions: [
          FlatButton(
            onPressed: () => Navigator.pop(context, false), // passing false
            child: Text('No'),
          ),
          FlatButton(
            onPressed: () => Navigator.pop(context, true), // passing true
            child: Text('Yes'),
          ),
        ],
      );
    }).then((exit) {
  if (exit == null) return;

  if (exit) {
    // user pressed Yes button
  } else {
    // user pressed No button
  }
});


답변

평면 버튼 클릭시 경고 대화 상자를 닫는 예

RaisedButton(
        onPressed: () {
          showDialog(
              context: context,
              builder: (context) => AlertDialog(
                    title: Text('Are you sure?'),
                    content: Text('Do you want to remove item?'),
                    actions: <Widget>[
                      FlatButton(
                          onPressed: () => Navigator.of(context).pop(false),//  We can return any object from here
                           child: Text('NO')),
                      FlatButton(
                          onPressed: () => Navigator.of(context).pop(true), //  We can return any object from here
                          child: Text('YES'))
                    ],
                  )).then((value) =>
              print('Selected Alert Option: ' + value.toString()));
        },
        child: Text('Show Alert Dialog'),
      ),

위의 코드에는 대화의 콜백 결과를 제공하는 데 사용되는 두 가지 고유 한 것이 있습니다.

Navigator.of (context) .pop (false)-NO를 눌렀을 때 거짓 값을 반환 Navigator.of (context) .pop (true)-YES를 눌렀을 때 참 값을 반환

이러한 반환 값을 기반으로 외부에서 일부 작업을 수행하거나 대화 상자 상태 값을 유지할 수 있습니다.


답변

이것은 완벽하게 작동합니다.

      RaisedButton(
                child: Text(
                  "Cancel",
                  style: TextStyle(color: Colors.white),
                ),
                color: Colors.blue,
                onPressed: () => Navigator.pop(context),
              ),


답변

AlertDialog를 비동기 메서드로 래핑하여 정리할 수 있습니다.

  _showAlertConfirmDelete() async {
    // the response will store the .pop value (it can be any object you want)
    var response = await showDialog(
        context: context,
        builder: (context) => AlertDialog(
              title: Text('Warn'),
              content: Text('Really wants to remove the record?'),
              actions: <Widget>[
                FlatButton(
                    onPressed: () => Navigator.of(context)
                        .pop(false),
                    child: Text('No')),
                FlatButton(
                    onPressed: () => Navigator.of(context).pop(true),
                    child: Text('Yes'))
              ],
            ));
    // do you want to do with the response.
    print(response);
  }