Flutter悬浮按钮FloatingActionButton使用详解

 更新时间:2021年07月12日 10:42:15   作者:sugood  
本文主要介绍了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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • 分析Android App中内置换肤功能的实现方式

    分析Android App中内置换肤功能的实现方式

    这篇文章主要介绍了Android App中内置换肤功能的实现方式,文中举了一个类似QQ空间中换肤方式的例子作为说明,需要的朋友可以参考下
    2016-02-02
  • Android ViewPager实现无限循环效果

    Android ViewPager实现无限循环效果

    这篇文章主要为大家详细介绍了Android ViewPager实现无限循环效果的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Android RecyclerView选择多个item的实现代码

    Android RecyclerView选择多个item的实现代码

    这篇文章主要为大家详细介绍了Android RecyclerView选择多个item的实现代码,仿网易新闻客户端频道选择效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android编程实现屏幕禁止休眠的方法

    Android编程实现屏幕禁止休眠的方法

    这篇文章主要介绍了Android编程实现屏幕禁止休眠的方法,分析了Android的Manifest.xml设置与代码实现两种操作技巧,需要的朋友可以参考下
    2016-10-10
  • Android入门之使用OKHttp组件访问网络资源

    Android入门之使用OKHttp组件访问网络资源

    这篇文章主要为大家详细介绍了Android如何使用OKHttp组件访问网络资源功能,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-12-12
  • Android NavigationBar问题处理的方法

    Android NavigationBar问题处理的方法

    本篇文章主要介绍了Android NavigationBar问题处理的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android获取周围WIFI热点服务

    Android获取周围WIFI热点服务

    这篇文章主要为大家详细介绍了Android获取周围WIFI热点服务的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • android之HttpPost&HttpGet使用方法介绍

    android之HttpPost&HttpGet使用方法介绍

    下文直接讲用法,先知道怎么用,再知道怎么回事,具体如下,感兴趣的朋友可以参考下哈
    2013-06-06
  • Android中FloatingActionButton实现悬浮按钮实例

    Android中FloatingActionButton实现悬浮按钮实例

    这篇文章主要介绍了Android中FloatingActionButton实现悬浮按钮实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • 修改Android中hosts文件的步骤详解

    修改Android中hosts文件的步骤详解

    有朋友问Android怎么修改Hosts?对于这个问题,由于手头并没有Android设备,所以只能从网上搜罗了方法并总结出来,下面这篇文章主要介绍了修改Android中hosts文件的步骤,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02

最新评论