默认情况下,Scaffold在应用程序运行时更改FAB时会将浮动动作按钮(FAB)动画化。

我怎样才能禁用这个动画?
文档引用FloatingActionButtonAnimator.scaling动画,当按钮发生更改时,该动画会缩放按钮:
/动画器将floatingActionButton移动到新的floatingActionButtonLocation。/如果为null,ScaffoldState将使用默认的动画器FloatingActionButtonAnimator.scaling。final FloatingActionButtonAnimator floatingActionButtonAnimator;
但是,没有迹象表明如何完全禁用缩放动画。
问题的完整示例代码:
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Timer _timer;
bool showFirst = true;
@override
void initState() {
_timer = Timer.periodic(new Duration(seconds: 2), (Timer t) {
setState(() {
showFirst = !showFirst;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
floatingActionButtonLocation: showFirst
? FloatingActionButtonLocation.centerDocked
: FloatingActionButtonLocation.endDocked,
floatingActionButton: Padding(
padding: EdgeInsets.only(top: 100.0),
child: Column(
children: <Widget>[
Text('Floating Action Button Title'),
showFirst
? FloatingActionButton.extended(
heroTag: 'unique',
icon: Icon(Icons.filter_1),
label: Text('First FAB'),
onPressed: () {},
)
: FloatingActionButton.extended(
heroTag: 'unique2',
icon: Icon(Icons.filter_2),
label: Text('Second FAB'),
onPressed: () {},
),
],
),
),
);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
}向每个FAB添加不同的英雄标记不会影响动画。
发布于 2018-12-30 18:00:19
您需要扩展FloatingActionButtonAnimator并重写其方法,检查以下代码,
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Timer _timer;
bool showFirst = true;
@override
void initState() {
_timer = Timer.periodic(new Duration(seconds: 2), (Timer t) {
setState(() {
showFirst = !showFirst;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
floatingActionButtonAnimator: NoScalingAnimation(),
floatingActionButtonLocation: showFirst
? FloatingActionButtonLocation.centerDocked
: FloatingActionButtonLocation.endDocked,
floatingActionButton: Padding(
padding: EdgeInsets.only(top: 100.0),
child: Column(
children: <Widget>[
Text('Floating Action Button Title'),
showFirst
? FloatingActionButton.extended(
heroTag: 'unique',
icon: Icon(Icons.filter_1),
label: Text('First FAB'),
onPressed: () {},
)
: FloatingActionButton.extended(
heroTag: 'unique2',
icon: Icon(Icons.filter_2),
label: Text('Second FAB'),
onPressed: () {},
),
],
),
),
);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
}
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);
}
}

您可以通过更改每个方法返回的内容来控制动画行为。为了前夫。您可以通过将getOffset方法更改为
@override
Offset getOffset({Offset begin, Offset end, double progress}) {
if (progress == 1.0){
return end;
}else{
return begin;
}
}发布于 2018-12-27 07:28:30
颤振中的floatingActionButtons具有名为heroTag的属性,每个floatingActionButtons都有相同的默认值。赋予每个floatingActionButtons唯一的heroTag将防止动画的发生。
Scaffold(
floatingActionButton: FloatingActionButton(
heroTag: "somethingUnique",
onPressed: () {},
child: Icon(Icons.add,),
),
body: Container(),
)https://stackoverflow.com/questions/53941252
复制相似问题