flutter - 对话框的动画背景颜色
问题描述
我创建了一个颜色补间动画来改变对话框的背景颜色,但它仍然没有改变。
我的代码:
void _showDialog() {
AnimationController? animationController = AnimationController(
vsync: this,
duration: const Duration(
milliseconds: 1000,
)
);
Animation<Color?>? animation = ColorTween(
begin: Colors.black54,
end: Colors.redAccent,
).animate(animationController!.view);
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
backgroundColor: animation!.value,
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)
),
child: MyTextWidget(
onSubmitted: (text) {
animationController.forward();
}
);
}
).then((value) => {
print(value)
});
}
然后我使用animationController.forward(),但没有任何反应。
我究竟做错了什么?
解决方案
There was no animation because you did not wrap your dialog with any widget that would animate. Please see the working code below the only thing I did was to wrap Dialog with a AnimatedBuider and call animationController.repeat(). You may call animationController.forward from your custom text widget :
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
//theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidgets(),
),
),
);
}
}
class MyWidgets extends StatefulWidget {
@override
_MyWidgetsState createState() => _MyWidgetsState();
}
class _MyWidgetsState extends State<MyWidgets> with TickerProviderStateMixin {
void _showDialog() {
AnimationController animationController = AnimationController(
vsync: this,
duration: const Duration(
milliseconds: 1000,
));
Animation<Color> animation = ColorTween(
begin: Colors.black54,
end: Colors.redAccent,
).animate(animationController.view);
showDialog(
context: context,
builder: (BuildContext context) {
animationController.repeat();
return AnimatedBuilder(
animation: animation,
builder: (context, snapshot) {
return Dialog(
backgroundColor: animation.value,
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
child: Container());
});
}).then((value) => {print(value)});
}
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
_showDialog();
//animationController
},
child: const Text("Show Dialog"),
);
}
}
推荐阅读
- javascript - 从控制面板按钮中删除边框
- c# - 如何从 Xamarin 中为 Android 密钥库生成的 KeyPair 获取指数/模数
- swift - 如何在运行时修改view.alphaValue并生效
- google-maps - 如何将当前位置数据发送到另一个活动?(科特林)
- javascript - 有没有办法在数据表的每一行上添加按钮?[反应js]
- java - 如何使用结果集?
- ios - 是否可以编写一个模拟 USB 键盘的 iPhone 应用程序?
- django - 使用函数更改 ModelSerializer 字段键
- java - 试图在邮递员上将日期作为路径变量传递但是我得到错误
- java - 每次进行工作区搜索后,Eclipse 都会开始构建工作区