my_scroll_view.dart 2.4 KB

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