经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Flutter » 查看文章
Flutter悬浮按钮FloatingActionButton使用详解
来源:jb51  时间:2021/7/12 13:14:04  对本文有异议

1、普通用法

  1. floatingActionButton: FloatingActionButton(
  2. child: Icon(Icons.add),
  3. onPressed: (){
  4. print('不要啊~');
  5. },
  6. ),

2、修改悬浮按钮位置

继承FloatingActionButtonLocation类,重写对应方法自定义位置

  1. import 'package:flutter/material.dart';
  2.  
  3. class CustomFloatingActionButtonLocation extends FloatingActionButtonLocation {
  4. FloatingActionButtonLocation location;
  5. double offsetX; // X方向的偏移量
  6. double offsetY; // Y方向的偏移量
  7. CustomFloatingActionButtonLocation(this.location, this.offsetX, this.offsetY);
  8.  
  9. @override
  10. Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
  11. Offset offset = location.getOffset(scaffoldGeometry);
  12. return Offset(offset.dx + offsetX, offset.dy + offsetY);
  13. }
  14. }
  15.  

使用

  1. floatingActionButtonLocation:CustomFloatingActionButtonLocation(
  2. FloatingActionButtonLocation.endFloat, 0, -DpUtils.setWidth(20)),

3、修改悬浮按钮大小

  1. floatingActionButton: SizedBox(
  2. height: 100.0,
  3. width: 100.0,
  4. child:FloatingActionButton(
  5. child: Icon(Icons.add),
  6. onPressed: () {},
  7. ),
  8. ),

4、去除悬浮按钮切换动画

继承FloatingActionButtonAnimator类并重写对应的方法

  1. import 'package:flutter/material.dart';
  2.  
  3. class NoScalingAnimation extends FloatingActionButtonAnimator{
  4. double _x;
  5. double _y;
  6. @override
  7. Offset getOffset({Offset begin, Offset end, double progress}) {
  8. _x = begin.dx +(end.dx - begin.dx)*progress ;
  9. _y = begin.dy +(end.dy - begin.dy)*progress;
  10. return Offset(_x,_y);
  11. }
  12.  
  13. @override
  14. Animation<double> getRotationAnimation({Animation<double> parent}) {
  15. return Tween<double>(begin: 1.0, end: 1.0).animate(parent);
  16. }
  17.  
  18. @override
  19. Animation<double> getScaleAnimation({Animation<double> parent}) {
  20. return Tween<double>(begin: 1.0, end: 1.0).animate(parent);
  21. }
  22. }
  23.  

使用

  1. floatingActionButtonAnimator: NoScalingAnimation(),

5、一般的自定义悬浮按钮样式

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. floatingActionButton: FloatingActionButton(
  5. child: Container(
  6. child: Column(
  7. mainAxisAlignment: MainAxisAlignment.center,
  8. crossAxisAlignment: CrossAxisAlignment.center,
  9. children: <Widget>[
  10. Image.asset(
  11. "images/float_button.png",
  12. width: DpUtils.setWidth(32),
  13. height: DpUtils.setWidth(32),
  14. ),
  15. Text(
  16. "悬浮按钮",
  17. style: TextStyle(fontWeight: FontWeight.bold,
  18. fontSize: DpUtils.setSp(20), color: Colors.white),
  19. ),
  20. ],
  21. ),
  22. ),
  23. elevation: 0,
  24. onPressed: () {
  25. _doSome();
  26. },
  27. backgroundColor: Colors.black,
  28. heroTag: "floatHome",
  29. ),
  30. )
  31. )}

6、彻底的自定义悬浮按钮样式

其实,floatingActionButton 可以直接传入普通的widget。所以该干嘛干嘛咯

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. floatingActionButton: Container(
  5. child: Column(
  6. mainAxisAlignment: MainAxisAlignment.center,
  7. crossAxisAlignment: CrossAxisAlignment.center,
  8. children: <Widget>[
  9. Image.asset(
  10. "images/home_icon_publishing.png",
  11. width: DpUtils.setWidth(32),
  12. height: DpUtils.setWidth(32),
  13. ),
  14. Text(
  15. "发主题",
  16. style: TextStyle(fontWeight: FontWeight.bold,
  17. fontSize: DpUtils.setSp(20), color: Colors.white),
  18. ),
  19. ],
  20. ),
  21. ),
  22. );
  23. }

到此这篇关于Flutter悬浮按钮FloatingActionButton使用详解的文章就介绍到这了,更多相关Flutter悬浮按钮FloatingActionButton内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue! 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号