【问题标题】:It is possible to know the fraction of a DraggableScrollableSheet shown on the screen?有可能知道屏幕上显示的 DraggableScrollableSheet 的分数吗?
【发布时间】:2021-06-15 07:23:49
【问题描述】:

我想知道是否有可能知道 DraggableScrollableSheet 在屏幕上占用了多少空间?我知道有 minChildSize 和 maxChildSize,所以我想在内部某个地方存储了一个实际的分数值。有没有办法听这个值?

【问题讨论】:

  • ScrollController scrollController 传递给builder - 很可能它可用于获取当前滚动偏移量
  • 不幸的是,当孩子滚动时,scrollController 是“活动的”。但是当工作表被拖动时,它什么也不做。有什么方法可以知道什么时候拖了多少工作表?
  • github.com/flutter/flutter/blob/f9c825981c/packages/flutter/lib/… - 我没有尝试过,但似乎你可以用NotificationListener捕捉到Notification
  • 是的!谢谢你。我在这里找到了我需要的东西:stackoverflow.com/questions/56506540/… - 感谢您的指导
  • 当然,欢迎您,一个建议:当不确定时,请务必查看源代码,就像我所做的那样 - 我之前不知道 DraggableScrollableNotification,但在阅读源代码 2 分钟后,我意识到它是在拖动过程中调度的

标签: flutter dart scroll draggable


【解决方案1】:

也许您可以使用 NotificationListener 及其方法 onNotification 来了解 DraggableScrollableSheet 的当前值。

import 'package:flutter/material.dart';
import 'package:payments/common/utils/colors.dart';
import 'package:payments/common/widgets/draggable_indicator.dart';

/// DraggableHistoryScreen class.
/// This class represents an screen for android and IOS
class DraggableHistoryScreen extends StatefulWidget{

  final double initialChildSize;

  /// Constructor method.
  DraggableHistoryScreen({
    required this.initialChildSize
  }); 

  @override
  _DraggableHistoryScreenState createState() => _DraggableHistoryScreenState();
}

/// _DraggableHistoryScreenState class.
/// This class represents the state for [DraggableHistoryScreen]
class _DraggableHistoryScreenState extends State<DraggableHistoryScreen>{
  

  
  @override
  Widget build(BuildContext context) {
    return Container(
        child: NotificationListener<DraggableScrollableNotification>(
          onNotification: (data) {
            //Here the data of your DraggableScrollableSheet
            print(data);
            return true;
          },
          child:  DraggableScrollableSheet(
            maxChildSize: 1.0,
            minChildSize: widget.initialChildSize,
            initialChildSize: widget.initialChildSize,
            builder: (context, scrollController) {
              return Stack(
                children: [
                  SingleChildScrollView(
                    controller: scrollController,
                    child: Container(
                        height:872.0,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(20.0),
                            topLeft: Radius.circular(20.0)),
                          color:AppColors.white_third_tone,
                        ),
                      ),
                    ),
                  Align(
                    alignment: Alignment.topCenter,
                    child: DraggableIndicator())
                ],
              );
            },
          ),
        )
      );
  }

}

【讨论】:

    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    相关资源
    最近更新 更多