my_scroll_view.dart
2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:keyboard_actions/keyboard_actions_config.dart';
/// 本项目通用的布局(SingleChildScrollView)
/// 1.底部存在按钮
/// 2.底部没有按钮
class MyScrollView extends StatelessWidget {
/// 注意:同时存在底部按钮与keyboardConfig配置时,为保证软键盘弹出高度正常。需要在`Scaffold`使用 `resizeToAvoidBottomInset: defaultTargetPlatform != TargetPlatform.iOS,`
/// 除非Android与iOS平台均使用keyboard_actions
const MyScrollView({
Key? key,
required this.children,
this.padding,
this.physics = const BouncingScrollPhysics(),
this.crossAxisAlignment = CrossAxisAlignment.start,
this.bottomButton,
this.keyboardConfig,
this.tapOutsideToDismiss = false,
this.overScroll = 16.0,
}): super(key: key);
final List<Widget> children;
final EdgeInsetsGeometry? padding;
final ScrollPhysics physics;
final CrossAxisAlignment crossAxisAlignment;
final Widget? bottomButton;
final KeyboardActionsConfig? keyboardConfig;
/// 键盘外部按下将其关闭
final bool tapOutsideToDismiss;
/// 默认弹起位置在TextField的文字下面,可以添加此属性继续向上滑动一段距离。用来露出完整的TextField。
final double overScroll;
@override
Widget build(BuildContext context) {
Widget contents = Column(
crossAxisAlignment: crossAxisAlignment,
children: children,
);
if (defaultTargetPlatform == TargetPlatform.iOS && keyboardConfig != null) {
/// iOS 键盘处理
if (padding != null) {
contents = Padding(
padding: padding!,
child: contents
);
}
contents = KeyboardActions(
isDialog: bottomButton != null,
overscroll: overScroll,
config: keyboardConfig!,
tapOutsideBehavior: tapOutsideToDismiss ? TapOutsideBehavior.opaqueDismiss : TapOutsideBehavior.none,
child: contents
);
} else {
contents = SingleChildScrollView(
padding: padding,
physics: physics,
child: contents,
);
}
if (bottomButton != null) {
contents = Column(
children: <Widget>[
Expanded(
child: contents
),
SafeArea(
child: bottomButton!
)
],
);
}
return contents;
}
}