【问题标题】:How can I make Flutter showModalBottomSheet animation faster?如何让 Flutter showModalBottomSheet 动画更快?
【发布时间】:2019-11-15 22:20:17
【问题描述】:

我想在我的应用中添加一个模态底页。我构建了要显示的小部件,但是,工作表动画对我来说有点太慢了。我想让它快一点。我没有找到关于动画速度、持续时间的任何信息,所以我不确定如何在 Flutter 的底部表单中实现更快的动画。

【问题讨论】:

  • 其固定为 200 毫秒,参见 material/bottom_sheet.dart - 导入后的第一行:const Duration _bottomSheetDuration = Duration(milliseconds: 200);
  • 我添加了一个类似问题的答案here

标签: flutter dart


【解决方案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),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2019-12-26
    • 2020-05-30
    • 2021-03-27
    • 2020-07-06
    • 2021-01-12
    • 2010-10-21
    • 1970-01-01
    • 2011-10-23
    • 2020-08-19
    相关资源
    最近更新 更多