You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击3D透视PageView中心图片跳转至指定页面?

实现3D透视PageView中心图片点击跳转

步骤1:定义每个图片的跳转目标

在_MyHomePageState类中添加列表,存储对应图片的跳转链接(顺序与PageView的itemCount一致):

final List<String> pageUrls = [
  "www.example1.com",
  "www.example2.com",
  "www.example3.com",
  "www.example4.com",
];

步骤2:给MyPage组件添加点击回调参数

修改MyPage的构造函数,新增点击回调参数:

class MyPage extends StatelessWidget {
  final number;
  final double? fraction;
  final VoidCallback? onTap; // 新增点击回调

  const MyPage({super.key, this.number, this.fraction, this.onTap});

  // ... 其余代码保持不变
}

步骤3:包裹图片并限制仅中心图片响应点击

在MyPage的Stack中,用GestureDetector包裹图片容器,并通过diff值判断是否为当前中心页面(避免滑动过程中误触):

return Stack(
  fit: StackFit.expand,
  alignment: FractionalOffset.center,
  children: [
    Transform.translate(
      offset: const Offset(0.0, -47.5),
      child: Transform(
        transform: pvMatrix,
        alignment: FractionalOffset.center,
        child: GestureDetector(
          // 仅当图片处于中心位置时响应点击(diff绝对值小于0.1)
          onTap: diff.abs() < 0.1 ? onTap : null,
          child: Container(
            decoration: BoxDecoration(boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.5),
                blurRadius: 11.0,
                spreadRadius: 4.0,
                offset: const Offset(13.0, 35.0),
              )
            ]),
            child: Image.asset(
              "assets/images/image_${number.toInt() + 1}.jpg",
              fit: BoxFit.fill,
            ),
          ),
        ),
      ),
    ),
  ],
);

步骤4:在PageView中传递点击回调

修改MyHomePage里的PageView.builder,为每个MyPage实例传递对应的跳转逻辑:

PageView.builder(
  controller: _controller,
  itemCount: 4,
  physics: const BouncingScrollPhysics(),
  itemBuilder: (context, index) {
    return MyPage(
      number: index.toDouble(),
      fraction: fraction,
      onTap: () => _launchURL(pageUrls[index]), // 绑定跳转方法
    );
  },
),

可选:跳转至App内部页面

如果需要跳转到App内的Flutter页面而非外部链接,将onTap替换为导航逻辑:

// 需提前导入目标页面文件
onTap: () => Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => DetailPage(itemIndex: index)),
),

内容的提问来源于stack exchange,提问作者loupdaniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:40:38