【问题标题】:Flutter Web allow overflow / bidirectional scrollingFlutter Web 允许溢出/双向滚动
【发布时间】:2021-03-15 01:16:29
【问题描述】:

在网络/桌面应用程序中,让容器同时支持垂直和水平滚动行为是很常见的。在玩过 Flutter Web 之后,我未能成功支持这个用例。 我能想到的最好方法是有一个UnconstrainedBox 允许内容尽可能大,并附加一个自定义滚动侦听器来检测垂直和水平滚动手势。 按照这种方法,我(正确地)看到了来自 Flutter 的溢出警告,但是,我明确希望允许在桌面或 Web 应用程序上溢出。如何明确告诉 Flutter 溢出问题不是

有没有更好的方法来解决这个问题? -- 我尝试使用与我想要的非常接近的InteractiveViewer 小部件,但是,我只能通过拖动内容而不是垂直或水平滚动来平移。

【问题讨论】:

    标签: flutter flutter-layout flutter-web


    【解决方案1】:

    同样的问题。

    您可以使用带有 minWidth 约束的 OverflowBox。它在屏幕上没有明显错误的情况下工作,但在控制台中造成绝对混乱......

    【讨论】:

      【解决方案2】:

      这是你想要的吗?

      Bidirectional scrolling with fixed scrollbar

      我使用adaptive_scrollbar v2.1.0 来获得这个结果,我使用了一个固定大小的容器,但它可能适用于许多不同的小部件。

      源代码

      import 'package:flutter/material.dart';
      import 'package:adaptive_scrollbar/adaptive_scrollbar.dart';
      
      void main() {
        runApp(const MyApp());
      }
      
      class MyApp extends StatelessWidget {
        const MyApp({Key? key}) : super(key: key);
      
        // This widget is the root of your application.
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            title: 'Flutter Bidirectional Scrollbars',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: const MyHomePage(title: 'Flutter Demo Home Page'),
          );
        }
      }
      
      class MyHomePage extends StatefulWidget {
        const MyHomePage({Key? key, required this.title}) : super(key: key);
      
        final String title;
      
        @override
        State<MyHomePage> createState() => _MyHomePageState();
      }
      
      class _MyHomePageState extends State<MyHomePage> {
        @override
        Widget build(BuildContext context) {
          final _verticalScrollController = ScrollController();
          final _horizontalScrollController = ScrollController();
      
          return Scaffold(
            appBar: AppBar(
              title: Text(widget.title),
            ),
            body: Container(
              height: 300,
              width: 700,
              child: AdaptiveScrollbar(
                underColor: Colors.blueGrey.withOpacity(0.3),
                sliderDefaultColor: Colors.grey.withOpacity(0.7),
                sliderActiveColor: Colors.grey,
                controller: _verticalScrollController,
                child: AdaptiveScrollbar(
                  controller: _horizontalScrollController,
                  position: ScrollbarPosition.bottom,
                  underColor: Colors.blueGrey.withOpacity(0.3),
                  sliderDefaultColor: Colors.grey.withOpacity(0.7),
                  sliderActiveColor: Colors.grey,
                  child: SingleChildScrollView(
                    controller: _verticalScrollController,
                    scrollDirection: Axis.vertical,
                    child: SingleChildScrollView(
                      controller: _horizontalScrollController,
                      scrollDirection: Axis.horizontal,
                      child: Padding(
                        padding: const EdgeInsets.only(right: 8.0, bottom: 16.0),
                        child: DataTable(
                          showCheckboxColumn: true,
                          columns: [
                            DataColumn(
                              label: Text('Name'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Name'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                            DataColumn(
                              label: Text('Year'),
                            ),
                          ],
                          rows: List<DataRow>.generate(
                            20,
                            (int index) => DataRow(
                              cells: <DataCell>[
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                                DataCell(
                                  Text('Row $index'),
                                ),
                              ],
                              onSelectChanged: (bool? value) {},
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          );
        }
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-08
        • 2019-08-13
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 2018-12-18
        • 1970-01-01
        相关资源
        最近更新 更多