flutter 在一个子树中有多个英雄共享同一个标签,我不明白这个问题

gijlo24d  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(175)

我想使用一个英雄widget。但是我得到了这个错误,我真的不知道错误在哪里:
在调度程序回调期间引发了以下Assert:在一个子树中有多个英雄共享同一个标签。
这是我使用Hero widget的代码的一部分:

class _Scrapbook extends State<Scrapbook> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.symmetric(vertical:8.0, horizontal: 0.0),
        child: Stack(
          children:[
            Positioned(
              top: 80,
                bottom: 20,
                left: 100,
                child: Text("Scrapbook", style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold))
            ),
          Positioned(
            top: 120,
            bottom: 0,
            width: MediaQuery.of(context).size.width,
            child: Column(
              children: [
                Expanded(child: GridView.builder(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 1.4,
                ),itemCount: 100,
                itemBuilder: (context, index) {
                  return InkWell(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(vertical:4.0, horizontal: 6.0),
                      child: Hero(
                        tag: "dash",
                        child: Card(
                          color: Colors.amber,
                          semanticContainer: true,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10.0),
                          ),
                          child: Column(
                            children: [
                              Text("POI"),
                              Flexible(child: Image.asset("assets/images/Rathaus.jpg")),
                            ],
                          ),
                        ),
                      ),
                    ),
                    onTap: () {
                      Navigator.push(context, MaterialPageRoute(builder: (_) => ScrapbookDetails()));
                    },
                  );
                },
                ))
              ],
            ),
          ),
         ],
        ),
      )
    );
  }
}

这里我再次使用它:

class ScrapbookDetails extends StatefulWidget {
  const ScrapbookDetails({super.key});

  @override
  State<ScrapbookDetails> createState() => _ScrapbookDetails();
}

class _ScrapbookDetails extends State<ScrapbookDetails> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child:Hero(tag: "dash",
              child: Image.asset("assets/images/Rathaus.jpg")),
      ),
    );
  }
}

如果你们能帮忙我会很感激的。感谢您的评分
我尝试了一个解决方案,从不同的职位,但它不是真的有帮助。

bjg7j2ky

bjg7j2ky1#

你的itemBuilder用相同的键重新调整英雄部件,

itemBuilder: (context, index) {
    return InkWell(
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical:4.0, horizontal: 6.0),
          child: Hero(
            tag: "dash",

当Hero键在小部件内时,您可以使用构造函数传递它。

itemBuilder: (context, index) {
    return InkWell(
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical:4.0, horizontal: 6.0),
          child: Hero(
            tag: "dash $index",

         .........
         Navigator.push(context, MaterialPageRoute(builder: (_) => ScrapbookDetails(heroKey:"dash $index")));
class ScrapbookDetails extends StatefulWidget {
  const ScrapbookDetails({super.key, required this.heroKey});
  final String heroKey;

  @override
  State<ScrapbookDetails> createState() => _ScrapbookDetails();
}

class _ScrapbookDetails extends State<ScrapbookDetails> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child:Hero(tag: widget.heroKey,
              child: Image.asset("assets/images/Rathaus.jpg")),
      ),
    );
  }
}

相关问题