能滑动隐藏的悬浮按钮
https://www.jianshu.com/p/18f6f6a532ec
https://www.jianshu.com/p/167ddc5660dc
const NotificationListener({
Key key,
@required this.child, 被监控的子widget树
this.onNotification, 监控到notification后的回调方法。
})
onNotification(ScrollNotification notification) , 此方法需要一个返回值,表示是否拦截住notification,如果是true,那么notifcation到此为止;如果是false,那么notification会继续向更外层widget传递。参数ScrollNotification包含了监听到的信息。
ScrollNotification
ScrollNotification({
@required this.metrics, 所有信息都在这里存储 ScrollMetrics
@required this.context,
});
ScrollController 常用方法
controller.addListener((){
print(controller......)
});
下面我们开始实现我们的效果
我们使用Scaffold floatingActionButton 来添加我们widget ,配合 floatingActionButtonLocation 设置下位置,
Scaffold(
appBar: PreferredSize(
child: _getAppBar(),
preferredSize: Size(double.infinity,44),
),
backgroundColor: Colors.white,
floatingActionButton: AnimationWidget(),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
body: ...,
);
创建悬浮按钮 AnimationWidget
import ‘package:flutter/material.dart‘;
import ‘package:provider/provider.dart‘;
//状态管理
class NotifierAnimation extends ChangeNotifier{
//回调方法
ValueChanged stopValueChanged;
void animationStartAndEnd(isStop){
stopValueChanged(isStop);
notifyListeners();
}
}
class AnimationWidget extends StatefulWidget {
@override
_AnimationWidgetState createState() => _AnimationWidgetState();
}
class _AnimationWidgetState extends State<AnimationWidget> with SingleTickerProviderStateMixin{
AnimationController _animationController;
Animation<Offset> _animation;
@override
void initState() {
super.initState();
//实例化动画
_animationController = new AnimationController(vsync: this,duration: new Duration(milliseconds: 240));
_animation = Tween(begin: Offset(0,0), end: Offset(0.8, 0)).animate(_animationController);
}
@override
void didChangeDependencies() {
//Provider 状态管理 得到回调如果true 那就是开始动画
final counter = Provider.of<NotifierAnimation>(context);
if (counter !=null) {
Provider.of<NotifierAnimation>(context,listen: false).stopValueChanged=((v){
if(v){
//开始动画
_animationController.forward();
}else{
//复位动画
_animationController.reverse();
}
});
}
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.centerRight,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 100),
SlideTransition(
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Colors.red
),
),position: _animation,),
],
)