[英]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.核心思想如下。
Column
of form contents like TextFormField
wrap them in ListView
widget with its children as form fields.TextFormField
这样的表单内容Column
,而是将它们包装在ListView
小部件中,其子项作为表单字段。ScrollController
as required by the DraggableScrollBar.xyz
of you choice and pass it this _myFormScrollController
.DraggableScrollBar.xyz
的要求创建一个ScrollController
并将其传递给_myFormScrollController
。_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 的一些注意事项:
TextEditingContorller
for each TextFormField
which is not advisable.TextFormField
创建一个新的TextEditingContorller
。 It serves only a cosmetic purpose in this example.MainScreen
widgetMainScreen
小部件中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.