首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何知道床单已经完全消失了?

如何知道床单已经完全消失了?
EN

Stack Overflow用户
提问于 2022-09-14 15:53:18
回答 3查看 29关注 0票数 0

如果调用带有等待前缀的showMaterialModalBottomSheet(....)

  • Click函数,则
  1. 调用脚本上的showMaterialModalBottomSheet以使工作表消失
  2. ,只有在工作表幻灯片开始时,才能在第一帧通知我。如何知道床单已经完全消失了?

如果你知道,请告诉我,谢谢。

EN

回答 3

Stack Overflow用户

发布于 2022-09-14 15:56:17

尝试使用showModalBottomSheetwhenComplete回调。

代码语言:javascript
运行
复制
showModalBottomSheet<void>(
        builder: (context) => Container(),
).whenComplete(() {
// Goes here
});
票数 0
EN

Stack Overflow用户

发布于 2022-09-14 16:31:12

如果您想知道输出动画何时完成,则需要使用动画控制器并将其附加到modalSheet。

late AnimationController _bottomSheetController;

附加控制器后,只需听动画状态,当工作表完全离开屏幕时,状态将被取消。

代码语言:javascript
运行
复制
@override
void initState() {
  super.initState();
  _bottomSheetController = AnimationController(
      vsync: this, duration: const Duration(milliseconds: 200))
    ..addStatusListener(_onListenerAnimation);
}

void _onListenerAnimation(AnimationStatus status) async {
  switch (status) {
    case AnimationStatus.dismissed:
      print('finished closing animation');
      break;
    case AnimationStatus.forward:
      print('starts opening sheet');
      break;
    case AnimationStatus.reverse:
      print('start closing the modal');
      break;
    case AnimationStatus.completed:
      print('finished opening animation');
      break;
  }
}

使用异步/等待或回调,您将在触发Navigator.of(context).pop()的同时得到结果。但是,如果您需要知道工作表何时离开屏幕,它将使用控制器。

FullCode

代码语言:javascript
运行
复制
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(primarySwatch: Colors.blue),
        home: const MyStateFullWidget());
  }
}

class MyStateFullWidget extends StatefulWidget {
  const MyStateFullWidget({Key? key}) : super(key: key);

  @override
  State<MyStateFullWidget> createState() => _MyStateFullWidgetState();
}

class _MyStateFullWidgetState extends State<MyStateFullWidget>
    with TickerProviderStateMixin {
  late AnimationController _bottomSheetController;

  @override
  void initState() {
    super.initState();
    _bottomSheetController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 200))
      ..addStatusListener(_onListenerAnimation);
  }

  @override
  void dispose() {
    super.dispose();
    _bottomSheetController
      ..removeStatusListener(_onListenerAnimation)
      ..dispose();
  }

  void _onListenerAnimation(AnimationStatus status) async {
    switch (status) {
      case AnimationStatus.dismissed:
        print('finished closing animation');
        break;
      case AnimationStatus.forward:
        print('starts opening sheet');
        break;
      case AnimationStatus.reverse:
        print('start closing the modal');
        break;
      case AnimationStatus.completed:
        print('finished opening animation');
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Demo')),
        body: SizedBox.expand(
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TextButton(
                  onPressed: _openBottomSheet, child: const Text('Open sheet'))
            ],
          ),
        ));
  }

  void _openBottomSheet() {
    showModalBottomSheet(
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10),
          topRight: Radius.circular(10),
        ),
      ),
      backgroundColor: Colors.white,
      context: context,
      transitionAnimationController: _bottomSheetController,
      isScrollControlled: false,
      builder: (context) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: [
            TextButton(
                onPressed: () => Navigator.of(context).pop(),
                child: const Text('Pop')),
          ],
        );
      },
    );
  }
}
票数 0
EN

Stack Overflow用户

发布于 2022-09-14 16:38:34

您可以随时等待返回,我相信这是知道您是从BottomSheet或页面返回最简单的方式。

示例:

代码语言:javascript
运行
复制
await showModalBottomSheet(
  context: context,
  builder: (context) => MyBottomSheet(),
);

doSomethinAfterAwait();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73719872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档