浮动按钮,一般用于Scaffold中,可摆放在特定位置。可盛放一个子组件,接收点击、可定义颜色、形状等。
相关组件
FloatingActionButton点击事件
【child】: 子组件 【Widget】
【tooltip】: 长按时提示文字 【String】
【backgroundColor】: 背景色 【Color】
【foregroundColor】: 前景色 【Color】
【elevation】: 影深 【double】
【onPressed】: 点击事件 【Function】
import 'package:flutter/material.dart';
class CustomFAB extends StatelessWidget {
@override
Widget build(BuildContext context) {
var data = {
Colors.red: Icons.add,
Colors.blue: Icons.bluetooth,
Colors.green: Icons.android,
};
return Wrap(
spacing: 20,
children: data.keys
.map((e) => FloatingActionButton(
heroTag: e.toString()+"a",
onPressed: () {},
backgroundColor: e,
foregroundColor: Colors.white,
child: Icon(data[e]),
tooltip: "android",
elevation: 5, //z-阴影盖度
))
.toList());
}
}
mini属性
【mini】: 是否是迷你 【bool】
动态效果参考图一
import 'package:flutter/material.dart';
class MiniFAB extends StatelessWidget {
@override
Widget build(BuildContext context) {
var data = {
Colors.red: Icons.add,
Colors.blue: Icons.bluetooth,
Colors.green: Icons.android,
};
return Wrap(
spacing: 20,
children: data.keys
.map((e) => FloatingActionButton(
heroTag: e.toString()+"b",
onPressed: () {},
backgroundColor: e,
mini: true,
foregroundColor: Colors.white,
child: Icon(data[e]),
tooltip: "android",
elevation: 5, //z-阴影盖度
))
.toList());
}
}
shape属性
【shape】: 形状 【ShapeBorder】
动态效果参考图一
import 'dart:math';
import 'package:flutter/material.dart';
import '../../../../app/utils/pather.dart';
class ShapeFAB extends StatelessWidget {
@override
Widget build(BuildContext context) {
var data = {
Colors.red: Icons.add,
Colors.blue: Icons.bluetooth,
Colors.green: Icons.android,
};
return Wrap(
spacing: 20,
children: data.keys
.map((e) => FloatingActionButton(
heroTag: e.toString()+"c",
onPressed: () {},
backgroundColor: e,
shape: StarBorder(),
foregroundColor: Colors.white,
child: Icon(data[e]),
tooltip: "android",
elevation: 5,
))
.toList());
}
}
/// 边线形状类
class StarBorder extends ShapeBorder {
@override
EdgeInsetsGeometry get dimensions => null;
@override
Path getInnerPath(Rect rect, {TextDirection textDirection}) {
return null;
}
@override
Path getOuterPath(Rect rect, {TextDirection textDirection}) {
return Pather.create.nStarPath(20, 25, 25 * cos((360 / 9 / 2) * pi / 180),
dx: rect.height / 2, dy: rect.width / 2);
}
@override
void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {}
@override
ShapeBorder scale(double t) {
return null;
}
}