题纲:
- WillPopScope
- GestureDetector
- RefreshIndicator
1.WillPopScope拦截、监听返回事件
初始化方法,其中onWillPop参数类型是一个Future<bool>的方法.
onWillPop该方法是可以实现安卓手机实体返回键的拦截、监听
const WillPopScope({ Key key, @required this.child, @required this.onWillPop, }) : assert(child != null), super(key: key); final Widget child; final WillPopCallback onWillPop; typedef WillPopCallback = Future<bool> Function();
//举个栗子 final _navKey = GlobalKey<NavigatorState>(); Future<bool>_onWillPop() async{ final b = await _navKey.currentState.maybePop(); //返回true:可以在主Router上返回 //fasel: 拦截返回事件 return Future.value(!b); }
2.GestureDetector手势监听
类似于iOS中UIControl,监听各种手势交互。
GestureDetector( onTap: ()=>{ setState(()=>{ _active = !_active }) }, onTapDown: (details) { //参数details.globalPosition,代表指针和屏幕的绝对位置 print(details.globalPosition); setState(()=>{ _hold = true }); } onTapUp: (details)=>{ setState(()=>{ _hold = false }) }, onTapCancel: ()=>{setState(()=>{ _hold = false }) }, //使用时必须传一个widget。 child: Container( alignment: Alignment.center, child: Text('GestureDetector'), width: 150, height: 150 ), )
3.RefreshIndicator上拉加载、下拉刷新控件
final _scrollV = ScrollController(); @override void initState() { super.initState(); //为ScrollController增加监视者 _scrollV.addListener(() { if (_scrollV.offset == _scrollV.position.maxScrollExtent && _scrollV.position.maxScrollExtent != 0.0) { loadMore(); } }); } { ... child:RefreshIndicator( //触发刷新偏移距离 displacement: 50, //触发刷新事件。返回值为Future onRefresh: dataInit, child: ListView.builder( controller: _scrollV, physics: const AlwaysScrollableScrollPhysics(), itemCount: items.length, itemBuilder: (content,i){ return ListTile( title: Text(items[i]), ); }, ), ) } Future<void> dataInit() async{ for(var i=0; i<15; i++){ items.add('Item$i'); } await Future.delayed((Duration(seconds: 2)), ()=>setState(()=>{ items })); }
发表评论