【问题标题】:Screen is popped off the stack using Navigator使用 Navigator 将屏幕从堆栈中弹出
【发布时间】:2020-04-02 16:40:30
【问题描述】:

我用几个项目填充了一个 Gridview,当我按下一个项目时,它会将我带到一个包含有关它的信息的详细信息页面。

但不知何故,当我按下后退按钮时,它不会返回上一页而是返回另一页,我相信它会自动从堆栈中弹出。

我想了解为什么会这样。

下面是包含网格视图的类:

 List<Container> containerSuperHero = new List();

  var posts = [
    {"title":"VIP Bartender", "imagem":"lib/images/logo.png"},
    {"title":"Flair Bartender", "imagem":"lib/images/logo.png"},
    {"title":"VIP Bartender", "imagem":"lib/images/logo.png"},
    {"title":"Flair Bartender", "imagem":"lib/images/logo.png"},
    {"title":"VIP Bartender", "imagem":"lib/images/logo.png"},
    {"title":"Flair Bartender", "imagem":"lib/images/logo.png"},
    {"title":"VIP Bartender", "imagem":"lib/images/logo.png"},
    {"title":"Flair Bartender", "imagem":"lib/images/logo.png"},
  ];

 _buildGridView(BuildContext context) async {

  for(var i=0; i < posts.length; i++){

    final posty = posts[i];
    final String image = posty["imagem"];

    containerSuperHero.add(
      new Container(
        padding: EdgeInsets.all(10.0),
        child: Card(
            child: Column(
            children: <Widget>[
              Hero(
                tag:posty,
                child: Material(
                child: InkWell(
                  onTap: ()=> Navigator.of(context).push(MaterialPageRoute(builder:
                  (BuildContext context) =>
                      Detail(title: posty["title"],
                        image: image,
                      ),
                  )
                  ),
                  child:  Image.asset(image,
                      height: 80,
                      width: 80,
                      fit: BoxFit.cover),
                ),
              )),
               Padding(padding: EdgeInsets.all(10.0)),
               Text(posty["title"],
                style: TextStyle(
                  fontSize: 18.0,
                ) ,)
            ],
          )
        )),
    );

  }
}

Widget buidContainerHero(BuildContext context){

   _buildGridView(context);

   return GridView.count(
       crossAxisCount: 2,
       children: containerSuperHero
   );
}


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

class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),),
      body: Stack(
          children: <Widget>[
            Column(
              children: <Widget>[
                _buildCarouselSlider(context),
                SizedBox(height: 30),
                Text("Welcome Back!"),
                SizedBox(height: 30),
                Expanded(
                    child: buidContainerHero(context))
              ],
            )
          ]
      ),
    );
  }
}

我的详细信息屏幕仅包含以下内容:

class Detail extends StatelessWidget {

  Detail(
      {this.postID, this.title, this.image}
      );

  final int postID;
  final String title;
  final String image;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ListView(
          children: <Widget>[
            Container(
              height: 240,
              child: Image.asset(image),
            )
          ],

        )
    );
  }
}

【问题讨论】:

    标签: flutter dart stack navigator scaffold


    【解决方案1】:

    试试这个, 导航器.push( 语境, MaterialPageRoute( builder: (_) => 详细信息(标题:posty["title"], 图像:图像, ), )

    【讨论】:

    • 你好!感谢您的回复,我已经尝试过了,但结果是一样的:/也许我可以覆盖 backButton 将我发送到上一个屏幕?
    • 尝试删除上下文 Navigator.of(context).push 我们已经在传递上下文只是用户 Navigator.push
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2012-10-14
    • 2022-01-20
    相关资源
    最近更新 更多