flutter - 参数类型“RevealedOffset”不能分配给参数类型“num”
问题描述
在使用我定义如下的类 EnsureVisibleWhenFocused 时出现此错误。我已经导入了正确的文件,并且标题中说明了我遇到的错误!
错误在viewport.getOffsetToReveal(object, 0.0)
和处viewport.getOffsetToReveal(object, 1.0)
。我试图找到解决方案,但在任何地方都找不到。
class EnsureVisibleWhenFocused extends StatefulWidget {
const EnsureVisibleWhenFocused({
Key key,
@required this.child,
@required this.focusNode,
this.curve: Curves.ease,
this.duration: const Duration(milliseconds: 100),
}) : super(key: key);
/// The node we will monitor to determine if the child is focused
final FocusNode focusNode;
/// The child widget that we are wrapping
final Widget child;
/// The curve we will use to scroll ourselves into view.
///
/// Defaults to Curves.ease.
final Curve curve;
/// The duration we will use to scroll ourselves into view
///
/// Defaults to 100 milliseconds.
final Duration duration;
EnsureVisibleWhenFocusedState createState() =>
new EnsureVisibleWhenFocusedState();
}
class EnsureVisibleWhenFocusedState extends State<EnsureVisibleWhenFocused>
{
@override
void initState() {
super.initState();
widget.focusNode.addListener(_ensureVisible);
}
@override
void dispose() {
super.dispose();
widget.focusNode.removeListener(_ensureVisible);
}
Future<Null> _ensureVisible() async {
// Wait for the keyboard to come into view
// TODO: position doesn't seem to notify listeners when metrics change,
// perhaps a NotificationListener around the scrollable could avoid
// the need insert a delay here.
await Future.delayed(const Duration(milliseconds: 300));
if (!widget.focusNode.hasFocus) return;
final RenderObject object = context.findRenderObject();
final RenderAbstractViewport viewport = RenderAbstractViewport.of(object);
assert(viewport != null);
ScrollableState scrollableState = Scrollable.of(context);
assert(scrollableState != null);
ScrollPosition position = scrollableState.position;
double alignment;
if (position.pixels > viewport.getOffsetToReveal(object, 0.0)) {
// Move down to the top of the viewport
alignment = 0.0;
} else if (position.pixels < viewport.getOffsetToReveal(object, 1.0)) {
// Move up to the bottom of the viewport
alignment = 1.0;
} else {
// No scrolling is necessary to reveal the child
return;
}
position.ensureVisible(
object,
alignment: alignment,
duration: widget.duration,
curve: widget.curve,
);
}
Widget build(BuildContext context) => widget.child;
解决方案
好的,我找到了解决方案。我们需要将 .offset 添加到那些 getOffsetToReveal 函数中。谢谢你
推荐阅读
- azure-functions - Function App 中的某些 Azure Functions 未运行
- laravel - 从 redis 'incrby' 中检索访问者数量并创建热门帖子
- c# - C#代理文件下载
- django - '未设置必需参数 {0}'.format(identifier))
- haskell - 如何组合将布尔值返回给一个函数的函数
- c++ - 为什么它打印五次 5 而不是 1 2 3 4 5?
- javascript - 使用计算值设置数据值时出现无效日期错误
- powershell - 让 PowerShell 将结果传递给 Pentaho
- angular - Angular 和 Blazor 客户端 sid 之间令人困惑的性能差距
- python-3.x - 如何通过python从filebeat索引中获取选定的日志