Flutter,我关闭的最后一页在我打开新页面时立即出现

huangapple go评论62阅读模式
英文:

Flutter, the last page I closed appears instantly when I open a new page

问题

当我在抽屉中切换页面时,先前的页面会出现1秒钟,然后关闭,我想要的页面才会出现。 https://youtube.com/shorts/YS5P2aQLBAM?feature=share

每当出现这个不需要的页面时,我会在控制台中看到以下错误信息:

->E/Surface (25496): getSlotFromBufferLocked: unknown buffer: 0xf46fec90

 @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: [
          //抽屉标题
          //抽屉主体
          Container(
            padding: const EdgeInsets.only(left: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                drawerItem(context, () {
                  Get.to(() => ProfileScreen());
                }, "Hesabım", Icons.person),
                drawerItem(context, () {
                  Get.to(() => MyAdress());
                }, "Adreslerim", Icons.location_on_sharp),
              ],
            ),
          ),
        ],
      ),
    );
  }

  GestureDetector drawerItem(
      BuildContext context, VoidCallback onTap, String? a, IconData icon) {
    return GestureDetector(
      onTap: onTap,
      child: ListTile(
        leading: Icon(icon, color: Colors.black54),
        title: Text(
          a.toString(),
          style: TextStyle(color: Colors.black54),
        ),
      ),
    );
  }
}
英文:

When I switch between pages in the drawer, the previous page appears for 1 second, then it closes and the page I want appears. https://youtube.com/shorts/YS5P2aQLBAM?feature=share

Every time this unwanted page appears, I get this error in the console

> ->E/Surface (25496): getSlotFromBufferLocked: unknown buffer: 0xf46fec90

 @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: [
          //Drawer Header
          //Drawer Body
          Container(
            padding: const EdgeInsets.only(left: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                drawerItem(context, () {
                  Get.to(() => ProfileScreen());
                }, "Hesabım", Icons.person),
                drawerItem(context, () {
                  Get.to(() => MyAdress());
                }, "Adreslerim", Icons.location_on_sharp),
              ],
            ),
          ),
        ],
      ),
    );
  }

  GestureDetector drawerItem(
      BuildContext context, VoidCallback onTap, String? a, IconData icon) {
    return GestureDetector(
      onTap: onTap,
      child: ListTile(
        leading: Icon(icon, color: Colors.black54),
        title: Text(
          a.toString(),
          style: TextStyle(color: Colors.black54),
        ),
      ),
    );
  }
}

答案1

得分: 1

你应该提取到 StatelessWidgets 中,而不是将小部件提取为函数。

我在Flutter GetX中有一个带导航抽屉的工作示例。你可以看一下这个:

class HomeView extends StatelessWidget {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade400,
      drawer: Drawer(
        child: ListView(
          children: [
            DrawerItem(
              icon: Icons.abc,
              onTap: () => Get.to(const DemoView()),
              title: "Home",
            ),
          ],
        ),
      ),
      body: const SizedBox.shrink(),
    );
  }
}

这是抽屉项目小部件:

class DrawerItem extends StatelessWidget {
  final String title;
  final VoidCallback onTap;
  final IconData icon;
  const DrawerItem({
    Key? key,
    required this.title,
    required this.onTap,
    required this.icon,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: ListTile(
        leading: Icon(icon, color: Colors.black54),
        title: Text(
          title,
          style: TextStyle(color: Colors.black54),
        ),
      ),
    );
  }
}
英文:

You should extract into StatelessWidgets instead of extracting widget as a function.

I have a working example of the drawer with navigation in Flutter GetX. You can take a look at this:

class HomeView extends StatelessWidget {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade400,
      drawer: Drawer(
        child: ListView(
          children: [
            DrawerItem(
              icon: Icons.abc,
              onTap: () => Get.to(const DemoView()),
              title: "Home",
            ),
          ],
        ),
      ),
      body: const SizedBox.shrink(),
    );
  }
}

Here's the drawer item widget

class DrawerItem extends StatelessWidget {
  final String title;
  final VoidCallback onTap;
  final IconData icon;
  const DrawerItem({
    Key? key,
    required this.title,
    required this.onTap,
    required this.icon,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: ListTile(
        leading: Icon(icon, color: Colors.black54),
        title: Text(
          title,
          style: TextStyle(color: Colors.black54),
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年2月8日 17:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定