当前位置:   article > 正文

Flutter 底部抽屉,三阶滑动;

flutter 底部抽屉

Flutter 底部抽屉,三阶滑动,但不跟随手指滑动、效果一般;Head布局可以触发抽屉滑动, 内部的Sliver布局单独滑动;

推荐看这篇文章,支持列表Sliver滑动;Flutter 底部列表抽屉,三阶滑动 , 支持列表Sliver布局_Choi晨的博客-CSDN博客

  

  1. import 'package:flutter/material.dart';
  2. class SlidingPanel3Controller {
  3. _SlidingPanel3ViewState? onState;
  4. _addState(_SlidingPanel3ViewState? onState){
  5. this.onState = onState;
  6. }
  7. void setPanel3State(Panel3State state){
  8. onState?.setPanel3state(state);
  9. }
  10. Panel3State getPanel3State(){
  11. return onState?._panel3state.value ?? Panel3State.CENTER;
  12. }
  13. }
  14. enum Panel3State { OPEN, CENTER, CLOSE, EXIT }
  15. class SlidingPanel3View extends StatefulWidget {
  16. final double heightOpen; //展开高度
  17. final double heightCenter; //中间高度
  18. final double heightClose; //闭合高度
  19. final Widget headWidget; //标题布局(内含滑动操作)
  20. final Widget bodyWidget; //内容布局
  21. final Panel3State initPanel3state; //初始状态
  22. final Color backColor; //背景色
  23. final SlidingPanel3Controller? slidingPanel3Controller; //控制器
  24. const SlidingPanel3View(
  25. {Key? key,
  26. this.heightOpen = 600,
  27. this.heightCenter = 360,
  28. this.heightClose = 100,
  29. required this.headWidget,
  30. required this.bodyWidget,
  31. this.slidingPanel3Controller,
  32. this.initPanel3state = Panel3State.CENTER,
  33. this.backColor = Colors.transparent})
  34. : super(key: key);
  35. @override
  36. State<SlidingPanel3View> createState() => _SlidingPanel3ViewState();
  37. }
  38. class _SlidingPanel3ViewState extends State<SlidingPanel3View> {
  39. double heightClose = 100;
  40. double heightCenter = 360;
  41. double heightOpen = 600;
  42. final ValueNotifier<Panel3State> _panel3state = ValueNotifier(Panel3State.CENTER);
  43. SlidingPanel3Controller? slidingPanel3Controller;
  44. @override
  45. void initState() {
  46. super.initState();
  47. heightClose = widget.heightClose;
  48. heightCenter = widget.heightCenter;
  49. heightOpen = widget.heightOpen;
  50. _panel3state.value = widget.initPanel3state;
  51. slidingPanel3Controller = widget.slidingPanel3Controller;
  52. slidingPanel3Controller?._addState(this);
  53. }
  54. void setPanel3state(Panel3State s){
  55. _panel3state.value = s;
  56. }
  57. double panelHeight() {
  58. if (_panel3state.value == Panel3State.OPEN) {
  59. return heightOpen;
  60. } else if (_panel3state.value == Panel3State.CENTER) {
  61. return heightCenter;
  62. } else if (_panel3state.value == Panel3State.CLOSE) {
  63. return heightClose;
  64. } else {
  65. return 0;
  66. }
  67. }
  68. @override
  69. Widget build(BuildContext context) {
  70. return ValueListenableBuilder(
  71. valueListenable: _panel3state,
  72. builder: (context, state, child) {
  73. return AnimatedContainer(
  74. color: widget.backColor,
  75. duration: const Duration(milliseconds: 220),
  76. width: double.infinity,
  77. height: panelHeight(),
  78. child: Column(
  79. children: [HeadView(), Expanded(child: widget.bodyWidget)],
  80. ),
  81. );
  82. },
  83. );
  84. }
  85. double pointerMove = 0;
  86. bool isCan = true;
  87. Widget HeadView() {
  88. return Listener(
  89. onPointerDown: (e) {
  90. pointerMove = e.position.dy;
  91. isCan = true;
  92. },
  93. onPointerMove: (e) {
  94. if (e.position.dy - pointerMove > 20 && isCan) {
  95. // print("手指下滑触发 -- ");
  96. isCan = false;
  97. if (_panel3state.value == Panel3State.OPEN) {
  98. _panel3state.value = Panel3State.CENTER;
  99. } else if (_panel3state.value == Panel3State.CENTER) {
  100. _panel3state.value = Panel3State.CLOSE;
  101. }
  102. } else if (e.position.dy - pointerMove < -20 && isCan) {
  103. // print("手指上滑触发 -- ");
  104. isCan = false;
  105. if (_panel3state.value == Panel3State.CLOSE) {
  106. _panel3state.value = Panel3State.CENTER;
  107. } else if (_panel3state.value == Panel3State.CENTER) {
  108. _panel3state.value = Panel3State.OPEN;
  109. }
  110. }
  111. },
  112. onPointerUp: (e) {
  113. isCan = true;
  114. },
  115. child: widget.headWidget,
  116. );
  117. }
  118. }

使用:

  1. SlidingPanel3Controller slidingPanel3Controller = SlidingPanel3Controller();
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. body: Stack(
  6. children: [
  7. InkWell(
  8. onTap: (){
  9. slidingPanel3Controller.setPanel3State(Panel3State.CLOSE);
  10. },
  11. child: Container(
  12. width: double.infinity,
  13. height: double.infinity,
  14. color: Colors.blue.withAlpha(88),
  15. ),
  16. ),
  17. Align(
  18. alignment: AlignmentDirectional.bottomCenter,
  19. child: SlidingPanel3View(
  20. heightClose: ScreenUtils.getDip(116),
  21. heightCenter: ScreenUtils.getDip(330),
  22. heightOpen: ScreenUtils.getDip(680),
  23. headWidget: headView(),
  24. bodyWidget: BodyView(),
  25. slidingPanel3Controller: slidingPanel3Controller,
  26. initPanel3state: Panel3State.CENTER,
  27. )),
  28. ],
  29. ),
  30. );
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/628215
推荐阅读
相关标签
  

闽ICP备14008679号