简体   繁体   English

表单颤动上的可拖动滚动条

[英]draggable scrollbar on form flutter

I'm trying to place a draggable scrollbar on my flutter Form , so the web users can click and drag to the screen bottom.我正在尝试在我的 flutter Form上放置一个可拖动的滚动条,以便网络用户可以单击并拖动到屏幕底部。 The flutter Scrollbar class is fine for touchscreen devices, for mouses is not! flutter Scrollbar类适用于触摸屏设备,但不适用于鼠标!

I've tried using draggable_scrollbar , however it only accepts ListView as child.我试过使用draggable_scrollbar ,但它只接受ListView作为孩子。

Here is my current code structure:这是我当前的代码结构:

    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('My form'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Scrollbar(
            child: SingleChildScrollView(
              child: Form(...),
            ),
          ),
        ),
      );
    }

In Draggable Scrollbar you can try using ListView.builder with only 1 item which is your form, as shown below.在 Draggable Scrollbar 中,您可以尝试使用ListView.builder只有 1 个项目,即您的表单,如下所示。

 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('My form'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Scrollbar( child: SingleChildScrollView( child: DraggableScrollbar.rrect( controller: myScrollController, child: ListView.builder( controller: myScrollController, itemCount: 1, itemBuilder: (context, index) { return Form(); }, ), ), ), ), ), ); }

This is achievable with draggable_scrollbar which you tried already.这可以通过您已经尝试过的draggable_scrollbar实现。 The idea is not to wrap the Form widget with it, but wrap its child widget which can be a list since Form expects a single child.这个想法不是用它包装Form小部件,而是包装它的子widget ,它可以是一个列表,因为Form需要一个孩子。 Here is working example available in this live dart-pad .这是此 live dart-pad 中可用的工作示例。

The core idea is as follows.核心思想如下。

  1. Create you Form widget as usual.像往常一样创建您的表单小部件。
  2. Instead of creating a Column of form contents like TextFormField wrap them in ListView widget with its children as form fields.而不是创建像TextFormField这样的表单内容Column ,而是将它们包装在ListView小部件中,其子项作为表单字段。
  3. Create a ScrollController as required by the DraggableScrollBar.xyz of you choice and pass it this _myFormScrollController .根据您选择的DraggableScrollBar.xyz的要求创建一个ScrollController并将其传递给_myFormScrollController
  4. Pass the same _myFormScrollController to the ListView 's controller parameter.将相同的_myFormScrollController传递给ListView的控制器参数。

This should enable a scrollable form with scrollbar.这应该启用带有滚动条的可滚动表单。

        Form(
          key: _formKey,
          child: DraggableScrollbar.rrect(  
            controller: _scrollController,
            alwaysVisibleScrollThumb: true,
            child: ListView(
              controller: _scrollController,
              children: [
                ...List<Widget>.generate(15, (i) => TextFormField(
                  controller: TextEditingController()..text='Field $i',
                  validator: (value) {
                    if (value.isEmpty) {
                      return 'Please enter some text';
                    }
                    return null;
                  },
                )),

              ],
            ),
          ),
        )

Some notes about the live dart-pad:关于 live dart-pad 的一些注意事项:

  • contains entire source of the draggable_scrollbar widget.包含 draggable_scrollbar 小部件的全部来源。 All credit goes to it developer .所有的功劳都归功于它的开发人员
  • The code creates a new TextEditingContorller for each TextFormField which is not advisable.该代码为每个不可取的TextFormField创建一个新的TextEditingContorller It serves only a cosmetic purpose in this example.在此示例中,它仅用于装饰目的。
  • The core logic is only in the MainScreen widget核心逻辑仅在MainScreen小部件中

Note: I completely copied the draggable scroll bar code into the dartpad as its not possible to import packages in dartpad yet.注意:我将可拖动的滚动条代码完全复制到 dartpad 中,因为它无法在 dartpad 中导入包。 🤷‍♂️ 🤷‍♂️

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM