本文主要介绍了Flutter悬浮按钮FloatingActionButton使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1、普通用法
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
print('不要啊~');
},
),
2、修改悬浮按钮位置
继承FloatingActionButtonLocation类,重写对应方法自定义位置
import 'package:flutter/material.dart';
class CustomFloatingActionButtonLocation extends FloatingActionButtonLocation {
FloatingActionButtonLocation location;
double offsetX; // X方向的偏移量
double offsetY; // Y方向的偏移量
CustomFloatingActionButtonLocation(this.location, this.offsetX, this.offsetY);
@override
Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
Offset offset = location.getOffset(scaffoldGeometry);
return Offset(offset.dx + offsetX, offset.dy + offsetY);
}
}
使用
floatingActionButtonLocation:CustomFloatingActionButtonLocation(
FloatingActionButtonLocation.endFloat, 0, -DpUtils.setWidth(20)),
3、修改悬浮按钮大小
floatingActionButton: SizedBox(
height: 100.0,
width: 100.0,
child:FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {},
),
),
4、去除悬浮按钮切换动画
继承FloatingActionButtonAnimator类并重写对应的方法
import 'package:flutter/material.dart';
class NoScalingAnimation extends FloatingActionButtonAnimator{
double _x;
double _y;
@override
Offset getOffset({Offset begin, Offset end, double progress}) {
_x = begin.dx +(end.dx - begin.dx)*progress ;
_y = begin.dy +(end.dy - begin.dy)*progress;
return Offset(_x,_y);
}
@override
Animation<double> getRotationAnimation({Animation<double> parent}) {
return Tween<double>(begin: 1.0, end: 1.0).animate(parent);
}
@override
Animation<double> getScaleAnimation({Animation<double> parent}) {
return Tween<double>(begin: 1.0, end: 1.0).animate(parent);
}
}
使用
floatingActionButtonAnimator: NoScalingAnimation(),
5、一般的自定义悬浮按钮样式
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.asset(
"images/float_button.png",
width: DpUtils.setWidth(32),
height: DpUtils.setWidth(32),
),
Text(
"悬浮按钮",
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: DpUtils.setSp(20), color: Colors.white),
),
],
),
),
elevation: 0,
onPressed: () {
_doSome();
},
backgroundColor: Colors.black,
heroTag: "floatHome",
),
)
)}
6、彻底的自定义悬浮按钮样式
其实,floatingActionButton 可以直接传入普通的widget。所以该干嘛干嘛咯
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.asset(
"images/home_icon_publishing.png",
width: DpUtils.setWidth(32),
height: DpUtils.setWidth(32),
),
Text(
"发主题",
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: DpUtils.setSp(20), color: Colors.white),
),
],
),
),
);
}
到此这篇关于Flutter悬浮按钮FloatingActionButton使用详解的文章就介绍到这了,更多相关Flutter悬浮按钮FloatingActionButton内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:Flutter悬浮按钮FloatingActionButton使用详解
基础教程推荐
猜你喜欢
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Android多返回栈技术 2023-04-15
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Android中的webview监听每次URL变化实例 2023-01-23
- android studio按钮监听的5种方法实例详解 2023-01-12
- Flutter手势密码的实现示例(附demo) 2023-04-11
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- iOS开发教程之XLForm的基本使用方法 2023-05-01
