Flutter web嵌套路由

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

Flutter web nested routing

问题

I am trying to set up Flutter web with a drawer on the left side containing 2 menus, and its respective pages on the right side. The drawer should always be open on the left side. When I click each button, only the right side should update. I achieved this with conditional checks, but it's not the right way because if I copy the URL and paste it elsewhere, it reloads. So, I want to set up proper routing. I tried using the go_router, but it opens a new page. Can someone suggest a way to achieve this?

class DashBoard extends StatelessWidget {
  const DashBoard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: const [
          Expanded(flex: 1, child: EnquiryDrawer()),
          Expanded(flex: 6, child: EnquiryListScreen())
        ],
      ),
    );
  }
}

Flutter web嵌套路由

I have shared the image; essentially, I want only the right-side widget to update, while the left side remains fixed.

英文:

hey i am trying to setup in flutter web , with a drawer on the left side with 2 menus and on the right side its respective pages, the drawer. is always open on left side ,when I click on each of button ,only right side should build, I achieved this by doing conditional check but its not right way as if I copy the URL and paste somewhere its reloaded, so I want to setup proper routing, I tried with go router, but all I get is new page, somebody suggest me a way to execute

  const DashBoard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: const [
          Expanded(flex: 1, child: EnquiryDrawer()),
          Expanded(flex: 6, child: EnquiryListScreen())
        ],
      ),
    );
  }
}

Flutter web嵌套路由

I have shared the image,basically what I want is only right side widget should build,left side should be fixed,

答案1

得分: 1

你可以通过将基础页面设置为堆栈来实现这一点:

class PageWrapper extends StatelessWidget {
  const PageWrapper(this.child, {Key? key}) : super(key: key);
  
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: [
            child,
            //const MenuDrawer(),
          ],
        ),
    );
  }
}

然后在GoRouter中,你可以将它用作ShellRoute:

GoRouter(
  routes: [
    ShellRoute(
      builder: (context, state, child) => PageWrapper(child),
      routes: [
        // 在这里添加你的路由
      ],
    ),
  ],
);

这将在PageWrapper类中构建ShellRoute的所有子路由。

英文:

You can achieve this by making your base page a stack:

class PageWrapper extends StatelessWidget {
  const PageWrapper(this.child, {Key? key}) : super(key: key);
  
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: [
            child,
            //const MenuDrawer(),
          ],
        ),
    );
  }
}

then in goRouter you can use this as ShellRoute:

 GoRouter(
   routes: [
    ShellRoute(
        builder: (context, state, child) => PageWrapper(child),
        routes:[
         //your routes here
        ]
    ],
);

This will build all child routes of the shellroute within the PageWrapper class

huangapple
  • 本文由 发表于 2023年5月22日 18:10:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305103.html
匿名

发表评论

匿名网友

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

确定