【发布时间】:2018-08-25 11:28:04
【问题描述】:
我尝试添加一个不会填满整个屏幕的PageView。
为此,我将 PageView 放入了 Column:
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
body: new Column(
children: <Widget>[
new SizedBox(height: 100.0, child: new Center(child: new Text("sticky header"))),
new Expanded(
child: new PageView(
children: <Widget>[
new Container(
color: Colors.red,
child: new Padding(
padding: const EdgeInsets.all(50.0),
child: new _Painter(),
),
),
new Container(
color: Colors.green,
child: new Padding(
padding: const EdgeInsets.all(50.0),
child: new _Painter(),
),
),
],
),
),
],
),
);
}
}
到目前为止有效。
每个PageView 都有一个_Painter,其中有一个RenderBox 来绘制东西。
我的问题来了:我使用handleEvent 方法检测拖动事件,但y 位置错误。可以看到画出来的线不是我触摸屏幕的地方(透明气泡)。
我该如何解决这个问题?我必须自己计算正确的y 位置吗?
您可以找到full source here。
更新
globalToLocal 解决了一半的问题,但我仍然必须在计算中包含填充。有没有办法获取小部件的填充?
void _handleDragUpdate(DragUpdateDetails details) {
final pos = globalToLocal(details.globalPosition);
_currentPath?.lineTo(pos.dx + 50.0, pos.dy + 50.0);
markNeedsPaint();
}
奖励积分
当我左右拖动PageView 时,我的_PainterRenderBox 忘记了画线。记住这条线的最佳位置在哪里?将它们存储在_Painter 或_MyHomePageState?
【问题讨论】: