【问题标题】:flutter display 3d objects flutter_3d_obj颤振显示 3d 对象 flutter_3d_obj
【发布时间】:2020-04-17 03:59:36
【问题描述】:

我已经设法使用/flutter_3d_obj 在颤振上显示 3d 对象。我现在正试图在颤动中获取点击点的坐标。我该怎么做。或者在颤动中显示 3d 对象的最佳方式是什么?并有能力得到一个点的坐标。

【问题讨论】:

  • 如果此插件不提供执行此操作的方法,则无法获取水龙头的 3D 坐标 - 在这种情况下,您必须将该功能添加到插件中。跨度>
  • @creativecreatorormaybenot 有办法做到这一点..即使它是原生的

标签: flutter canvas 3d blender


【解决方案1】:

您可以用GestureDectector Widget 包裹您的3Dmodel Widget,并使用onTapDownDetail 属性来监听触摸位置

import 'package:flutter/material.dart';
import 'package:flutter_3d_obj/flutter_3d_obj.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('3D Touchable Model'),
        ),
        body: Touch3DModel());
  }
}

class Touch3DModel extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Touch3DModelState();
  }
}

class Touch3DModelState extends State<Touch3DModel> {
  double obj_width = 100;
  double obj_height = 100; 
  double x_coord = 100.0;
  double y_coord = 100.0;

  void onTapDown(BuildContext context, TapDownDetails details) {
    print('${details.globalPosition}');
    final RenderBox box = context.findRenderObject();
    final Offset localOffset = box.globalToLocal(details.globalPosition);
    setState(() {
      x_coord = localOffset.dx;
      y_coord = localOffset.dy;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (TapDownDetails details) => onTapDown(context, details),
      child: Stack(fit: StackFit.expand, children: <Widget>[
        Container(color: Colors.grey),
        Positioned(
          child: Object3D(
            size: const Size(obj_width, obj_height),
            path: "assets/file.obj",
            asset: true,
          ),
          left: x_coord - obj_width / 2,  // To offset the object possitioning from top left of the object to the center. 
          top: y_coord - obj_height / 2,
        )
      ]),
    );
  }
}

注意:使用 flutter_3d_obj 需要你有一个用搅拌器或类似工具构建的 3d obj。

感谢aptik

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2021-05-30
    • 2022-04-28
    • 2020-11-15
    相关资源
    最近更新 更多