Flutter Navigator弹出目标页面

时间:2019-06-05 02:57:54

标签: flutter pop navigator

例如 1.接口1在后台运行,请求结束后显示对话框A 2.界面2在前景中运行,并直接显示对话框B。结束请求后关闭对话框B

问题是对话框A在对话框B之后显示,但是在使用Navigator pop时关闭了对话框A,但是我需要关闭对话框B

那我该如何弹出目标页面?

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

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

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

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  StreamController<bool> sc = StreamController();

  Stream<bool> get stream => sc.stream;

  @override
  void initState() {
    stream.listen((show) {
      if (show) {
        showDialogB();
      } else {
        Navigator.of(context).pop();
      }
    });

    request1();
    request2();
    super.initState();
  }

  void request1() {
    Timer(Duration(seconds: 3), () {
      showDialogA();
    });
  }

  void showDialogA() {
    showDialog(
        context: context,
        builder: (context) {
          return Container(
            height: 100,
            width: 100,
            color: Colors.white,
            child: Center(
              child: Text(
                "新版本",
                style: TextStyle(color: Colors.black),
              ),
            ),
          );
        });
  }

  void showDialogB() {
    showDialog(
        context: context,
        builder: (context) {
          return Container(
            height: 100,
            width: 100,
            color: Colors.white,
            child: Center(
              child: CupertinoActivityIndicator(),
            ),
          );
        });
  }

  void request2() {
    sc.add(true);
    Timer(Duration(seconds: 5), () {
      //dimisssDialogB
      sc.add(false);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("111"),
      ),
    );
  }
}

0 个答案:

没有答案