无法将参数类型“ RevealedOffset”分配给参数类型“ num”

时间:2018-11-10 15:12:21

标签: flutter flutter-layout

在使用我定义如下的类确保可见性时出现此错误。 我已经导入了正确的文件,标题中指出了我得到的错误!

错误位于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;

2 个答案:

答案 0 :(得分:4)

好的,我找到了解决方案。 我们需要将.offset添加到这些getOffsetToReveal函数中。 谢谢

答案 1 :(得分:0)

我们需要传递函数viewport.getOffsetToReveal的偏移值

viewport.getOffsetToReveal(object, 1.0).offset