網(wǎng)站首頁 編程語言 正文
使用
通過監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)DragOpenDrawer 組件,可以給滾動(dòng)組件添加一個(gè)下拉抽屜。其使用方式如下:
DragOpenDrawer( ? openDuration: Duration(microseconds: 900), ? closeDuration: Duration(milliseconds: 300), ? onOpen: (){ ? ? print("onOpen"); ? }, ?child: Column( ? ? ? children: [ ? ? ? ? Expanded( ? ? ? ? ? child: ListView.builder( ? ? ? ? ? ? ? itemCount: 40, ? ? ? ? ? ? ? itemBuilder: (context,index){ ? ? ? ? ? ? return ListTile(title: Text("$index"),); ? ? ? ? ? }), ? ? ? ? ), ? ? ? ] ? ? ), ? backgroundBuilder: (context){ ? ? return Container(child: FlutterLogo(style: FlutterLogoStyle.stacked,),color: Colors.blue[200],); ? }, ),
組件參數(shù)說明
- openDuration:抽屜打開動(dòng)畫持續(xù)的時(shí)間
- closeDuration: 抽屜關(guān)閉動(dòng)畫持續(xù)的時(shí)間
- onOpen: 抽屜打開事件回調(diào)
- child: DragOpenDrawer 組件監(jiān)聽的滾動(dòng)組件
- backgroundBuilder:抽屜打開后展示的組件
運(yùn)行效果
源碼
import 'package:flutter/material.dart'; enum _DragOpenDrawerMode{ ? // 正在拖動(dòng) ? dragging, ? // 抽同打開事件已經(jīng)觸發(fā) ? done, ? // 抽屜處于關(guān)閉狀態(tài) ? canceled, ? // 抽屜已經(jīng)打開了 ? opened, } class DragOpenDrawer extends StatefulWidget { ? const DragOpenDrawer({ ? ? required this.child, ? ? required this.backgroundBuilder, ? ? this.onOpen, ? ? this.openDuration = ?const Duration(seconds: 1), ? ? this.closeDuration = const Duration(seconds: 1), ? ? Key? key}) : super(key: key); ? final Widget Function(BuildContext context) backgroundBuilder; ? final Widget ?child; ? /// 抽屜打開時(shí)的回調(diào)函數(shù) ? final void Function()? onOpen; ? final Duration openDuration; ? final Duration closeDuration; ? @override ? _DragOpenDrawerState createState() => _DragOpenDrawerState(); } class _DragOpenDrawerState extends State<DragOpenDrawer> with SingleTickerProviderStateMixin { ? late AnimationController _controller; ? late double _maxHeight; ? double _dragOffset = .0; ? bool _openTriggered = false; ? _DragOpenDrawerMode _dragOpenDrawerMode = _DragOpenDrawerMode.canceled; ? @override ? void initState() { ? ? super.initState(); ? ? _controller = AnimationController(vsync: this); ? } ? @override ? void dispose() { ? ? _changeDragOpenDrawerMode(_DragOpenDrawerMode.canceled); ? ? _controller.dispose(); ? ? super.dispose(); ? } ? @override ? Widget build(BuildContext context) { ? ? return LayoutBuilder( ? ? ? builder: (BuildContext context, BoxConstraints constraints) { ? ? ? ? _maxHeight = constraints.maxHeight; ? ? ? ? return ?WillPopScope( ? ? ? ? ? onWillPop: () async{ ? ? ? ? ? ? if(_dragOpenDrawerMode == _DragOpenDrawerMode.opened){ ? ? ? ? ? ? ? _changeDragOpenDrawerMode(_DragOpenDrawerMode.canceled); ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? ? ? return true; ? ? ? ? ? }, ? ? ? ? ? child: Stack( ? ? ? ? ? ? alignment: Alignment.topCenter, ? ? ? ? ? ? children: [ ? ? ? ? ? ? ? SizedBox( ? ? ? ? ? ? ? ? width: double.infinity, ? ? ? ? ? ? ? ? height: double.infinity, ? ? ? ? ? ? ? ? child: ScaleTransition( ? ? ? ? ? ? ? ? ? ? alignment: Alignment.topCenter, ? ? ? ? ? ? ? ? ? ? scale: _controller, ? ? ? ? ? ? ? ? ? ? child: widget.backgroundBuilder(context)), ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? AnimatedBuilder( ? ? ? ? ? ? ? ? animation: _controller, ? ? ? ? ? ? ? ? builder: (BuildContext context, Widget? child) { ? ? ? ? ? ? ? ? ? return ?Positioned( ? ? ? ? ? ? ? ? ? ? top: Tween(begin: .0, end: _maxHeight).evaluate(_controller), ? ? ? ? ? ? ? ? ? ? height: _maxHeight, ? ? ? ? ? ? ? ? ? ? width: constraints.maxWidth, ? ? ? ? ? ? ? ? ? ? child: NotificationListener( ? ? ? ? ? ? ? ? ? ? ? ? onNotification: (notification){ ? ? ? ? ? ? ? ? ? ? ? ? ? if(notification is OverscrollNotification){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(notification.overscroll >= 0){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _dragOffset -= notification.overscroll; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _changeDragOpenDrawerMode(_DragOpenDrawerMode.dragging); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(_dragOffset >_maxHeight/4){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _changeDragOpenDrawerMode(_DragOpenDrawerMode.done); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? }else if(notification is ScrollEndNotification && _dragOpenDrawerMode != _DragOpenDrawerMode.done){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? _controller ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ..duration = widget.closeDuration ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ..reverse().then((value) => _dragOffset = .0); ? ? ? ? ? ? ? ? ? ? ? ? ? }else if(notification is ScrollEndNotification && _dragOpenDrawerMode == _DragOpenDrawerMode.done){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? _changeDragOpenDrawerMode(_DragOpenDrawerMode.opened); ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? child: child ?? SizedBox()), ? ? ? ? ? ? ? ? ? ); ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? child:Container( ? ? ? ? ? ? ? ? ? color: Colors.white, ? ? ? ? ? ? ? ? ? height: _maxHeight, ? ? ? ? ? ? ? ? ? child: widget.child ? ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? ), ? ? ? ? ? ? ], ? ? ? ? ? ), ? ? ? ? ); ? ? ? }, ? ? ); ? } ? _changeDragOpenDrawerMode(_DragOpenDrawerMode newMode)async{ ? ? _dragOpenDrawerMode = newMode; ? ? switch (newMode){ ? ? ? case _DragOpenDrawerMode.canceled : { ? ? ? ? _controller.duration = widget.closeDuration; ? ? ? ? await _controller.reverse(); ? ? ? ? _openTriggered = false; ? ? ? ? _dragOffset = .0; ? ? ? ? break; ? ? ? } ? ? ? case _DragOpenDrawerMode.dragging: ? ? ? ? _controller.duration = Duration(seconds: 0); ? ? ? ? await ?_controller.animateTo(_dragOffset/_maxHeight); ? ? ? ? break; ? ? ? case _DragOpenDrawerMode.opened: ? ? ? ? _controller.duration = widget.openDuration; ? ? ? ? await _controller.forward(); ? ? ? ? break; ? ? ? case _DragOpenDrawerMode.done: ? ? ? ? if(!_openTriggered){ ? ? ? ? ? widget.onOpen!.call(); ? ? ? ? } ? ? ? ? _openTriggered = true; ? ? ? ? break; ? ? ? default: ? ? ? ? //executeUnknown(); ? ? } ? } }
原文鏈接:https://juejin.cn/post/7064012496956293133
相關(guān)推薦
- 2022-07-31 oracle定時(shí)任務(wù)定時(shí)無效的原因分析與解決_oracle
- 2022-03-05 CentOS系統(tǒng)下安裝及配置JDK介紹_Linux
- 2022-06-28 C++哈希表之線性探測(cè)法實(shí)現(xiàn)詳解_C 語言
- 2022-06-20 基于C#實(shí)現(xiàn)語音識(shí)別功能詳解_C#教程
- 2022-04-27 Django與DRF結(jié)合的全局異常處理方案詳解_python
- 2022-11-13 pandas進(jìn)階教程之Dataframe的apply方法_python
- 2022-03-29 C#算法之兩數(shù)之和_C#教程
- 2022-06-13 golang下grpc框架的使用編寫示例_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支