如何在 Flutter Web 上将小部件固定到 ListView 的右侧

发布于 2025-01-15 04:23:21 字数 2107 浏览 3 评论 0原文

我有一个 Flutter Web 应用程序,当我单击某个项目时,我需要在 ListView 的右侧显示一个小部件,并且该小部件应始终在屏幕上可见。我可以实现我的目标,将两者放在 Row 上并仅对 ListView 使用可滚动,但这需要 ListView 由具有定义高度的小部件包裹。

当我调整浏览器大小时,定义一个具有高度的容器来包裹 ListView 会破坏响应能力,因为该容器不适合屏幕的高度。

我想到使用 ListView 的 ShrinkWrap 属性,这样我就不必将其包装在具有预定义高度的小部件中,但这使得整个 Row 可以垂直滚动,最终导致小部件离开视口。

如果有人知道如何将这个右侧小部件固定在屏幕上,以便我可以在不失去响应能力的情况下实现我的目标,我将不胜感激。

这与我到目前为止所得到的类似:

class PageLayout extends StatefulWidget {
  const PageLayout({Key? key, required this.items}) : super(key: key);

  final List<String> items;

  @override
  State<PageLayout> createState() => _PageLayoutState();
}

class _PageLayoutState extends State<PageLayout> {
  final rightSideWidget = Container(
      decoration: BoxDecoration(
        color: Colors.red,
        border: Border.all(color: Colors.white, width: 2),
      ),
      height: 200);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              width: MediaQuery.of(context).size.width * 0.49,
              child: ListView.builder(
                shrinkWrap: true,
                itemBuilder: (context, index) => Container(
                  decoration: BoxDecoration(
                    color: Colors.blue,
                    border: Border.all(color: Colors.white, width: 2),
                  ),
                  height: 200,
                  child: Center(
                    child: Text(
                      widget.items[index],
                      style: const TextStyle(color: Colors.white),
                    ),
                  ),
                ),
                itemCount: widget.items.length,
              ),
            ),
            Expanded(child: rightSideWidget),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

我希望 rightSideWidget 始终位于屏幕中央或跟随滚动。

I have a Flutter Web application where I need to show a widget on the right side of a ListView when I click an item and this widget should always be visible on screen. I can achieve my objective puting both on a Row and using a scrollable only for the ListView, but that requires the ListView to be wrapped by a widget with defined height.

Defining a container with height to wrap the ListView breaks the responsiveness when I resize the browser, as the container doesn't fit the height of the screen.

I thought of using the shrinkWrap property of the ListView so I don't have to wrap it in a widget with predefined height, but that makes the whole Row scrollable vertically, eventually causing the widget to leave the viewport.

I would appreciate if somebody knows how could I keep this right side widget fixed on screen so I can achieve my objective without losing responsiveness.

Here's something similitar to what I've got so far:

class PageLayout extends StatefulWidget {
  const PageLayout({Key? key, required this.items}) : super(key: key);

  final List<String> items;

  @override
  State<PageLayout> createState() => _PageLayoutState();
}

class _PageLayoutState extends State<PageLayout> {
  final rightSideWidget = Container(
      decoration: BoxDecoration(
        color: Colors.red,
        border: Border.all(color: Colors.white, width: 2),
      ),
      height: 200);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              width: MediaQuery.of(context).size.width * 0.49,
              child: ListView.builder(
                shrinkWrap: true,
                itemBuilder: (context, index) => Container(
                  decoration: BoxDecoration(
                    color: Colors.blue,
                    border: Border.all(color: Colors.white, width: 2),
                  ),
                  height: 200,
                  child: Center(
                    child: Text(
                      widget.items[index],
                      style: const TextStyle(color: Colors.white),
                    ),
                  ),
                ),
                itemCount: widget.items.length,
              ),
            ),
            Expanded(child: rightSideWidget),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

I want rightSideWidget to be always centered on screen or follow the scroll.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2025-01-22 04:23:21

您可以将屏幕分为两部分,右部分和左部分;从而能够控制两个部分中小部件的行为。

  • 使用 Row 将整个屏幕分成 2 个按比例的部分
    将此行小部件
  • 放入高度等于屏幕高度的容器内,以保持响应能力 |使用 MediaQuery 获取页面的当前高度
  • 现在左侧部分可以单独滚动,并且单击此部分中的任何选项时,您可以定义右侧部分的行为;同时在整个页面生命周期中保持左侧部分不变

You can divide your screen into two sections, right section and left section; thereby being able to control behaviour of widgets in both sections.

  • Divide the overall screen into 2 proportional sections using a Row
    widget
  • Put this Row widget inside a Container with height equal to screen height for preserving responsiveness | Use MediaQuery to get current height of page
  • Now left hand section can individually scroll, and on click of any option from this section you can define behaviour for right section; while keeping the left section constant throughout page lifecycle
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文