如何让 Flutter showModalBottomSheet 动画更快?
Posted
技术标签:
【中文标题】如何让 Flutter showModalBottomSheet 动画更快?【英文标题】:How can I make Flutter showModalBottomSheet animation faster? 【发布时间】:2019-11-15 22:20:17 【问题描述】:我想在我的应用中添加一个模态底页。我构建了要显示的小部件,但是,工作表动画对我来说有点太慢了。我想让它快一点。我没有找到关于动画速度、持续时间的任何信息,所以我不确定如何在 Flutter 的底部表单中实现更快的动画。
【问题讨论】:
其固定为 200 毫秒,参见material/bottom_sheet.dart
- 导入后的第一行:const Duration _bottomSheetDuration = Duration(milliseconds: 200);
我添加了一个类似问题的答案here
【参考方案1】:
您可以使用 AnimationController 配置 showModalBottomSheet 的 transitionAnimationController
来更新其持续时间(展开)和反转持续时间(收回)。这是一个演示
import 'package:flutter/material.dart';
void main()
runApp(const MyApp());
class MyApp extends StatelessWidget
const MyApp(Key? key) : super(key: key);
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
const MyHomePage(Key? key, required this.title) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin
late AnimationController controller;
@override
initState()
super.initState();
// Initialize AnimationController
initController();
@override
void dispose()
controller.dispose();
super.dispose();
void initController()
controller = BottomSheet.createAnimationController(this);
controller.duration = const Duration(milliseconds: 100);
controller.reverseDuration = const Duration(milliseconds: 100);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: const Center(
child: Text('Click button to show bottom sheet'),
),
floatingActionButton: FloatingActionButton(
onPressed: () => showModalBottomSheet(
transitionAnimationController: controller,
context: context,
builder: (builder)
return const Center(
child: Text("Bottom sheet"),
);
).whenComplete(() => initController()), // Initialize AnimationController after the controller has been disposed
tooltip: 'Bottom Sheet',
child: const Icon(Icons.arrow_upward_sharp),
),
);
【讨论】:
以上是关于如何让 Flutter showModalBottomSheet 动画更快?的主要内容,如果未能解决你的问题,请参考以下文章
如何让 Flutter showModalBottomSheet 动画更快?