flutter 从函数更新小部件的着色装饰

luaexgnf  于 2022-12-27  发布在  Flutter
关注(0)|答案(1)|浏览(138)

我现在有了一个更新当前用户位置的函数,这个函数工作正常,但问题是绘制的指针仍然停留在那个精确的位置,因为装饰只在小部件内部绘制过一次,永远不会改变。
我已经研究了Google是如何使用标记对象的,但这似乎对我的应用程序不起作用,因为我没有使用法线贴图。

void updateLocationPin(LocationData newLocalData, Uint8List imageData){
     LatLng latLng = LatLng(newLocalData.latitude!, newLocalData.longitude!);
     this.setState(() {
      /* marker = Marker(
         markerId: MarkerId("home"),
         position: latLng,
         rotation: 0.5,
         draggable: false,
         flat: true,
         zIndex: 2,
         anchor: Offset(0.5, 0.5),
         icon: BitmapDescriptor.fromBytes(imageData)
       );*/
       xPosition = latLng.longitude; 
       yPosition = latLng.latitude; 
       ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Position: ${xPosition}, ${yPosition}"), duration: Duration(milliseconds: 5000), ), );
     });
   }

  @override
  Widget build(BuildContext context) {
    var dpr = MediaQuery.of(context).devicePixelRatio;
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: [
            GestureDetector(
              child: InteractiveViewer(
                transformationController: controller,
                constrained: false,
                minScale: 0.1,
                maxScale: 1.0,
                child: Stack(children: [
                  Image.asset(
                    "assets/images/$level",
                  ),
                  Positioned(
                    left: xPosition,
                    top: yPosition,
                    child: Container(
                      width: 50.0 / dpr,
                      height: 50.0 / dpr,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: Colors.red,
                      ),
                    ),
                  ),
                ]),
                boundaryMargin: EdgeInsets.all(100),
              ),
            ),
          ],
        ),
      ),
    );
  }

每次调用updateLocationPin时,我该如何更新画圈呢?我应该事先指出这一点,但我是一个完全的初学者,试图通过自己编写代码来学习,如果有人指出我可能遗漏的任何问题或错误的代码,我将非常感激。

3mpgtkmj

3mpgtkmj1#

试试这个:

Color _newColor = Colors.red;
 void updateLocationPin(LocationData newLocalData, Uint8List imageData){
     LatLng latLng = LatLng(newLocalData.latitude!, newLocalData.longitude!);
     this.setState(() {
      /* marker = Marker(
         markerId: MarkerId("home"),
         position: latLng,
         rotation: 0.5,
         draggable: false,
         flat: true,
         zIndex: 2,
         anchor: Offset(0.5, 0.5),
         icon: BitmapDescriptor.fromBytes(imageData)
       );*/
       xPosition = latLng.longitude; 
       yPosition = latLng.latitude;
       _newColor = Colors.blue;
       ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Position: ${xPosition}, ${yPosition}"), duration: Duration(milliseconds: 5000), ), );
     });
   }

  @override
  Widget build(BuildContext context) {
    var dpr = MediaQuery.of(context).devicePixelRatio;
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: [
            GestureDetector(
              child: InteractiveViewer(
                transformationController: controller,
                constrained: false,
                minScale: 0.1,
                maxScale: 1.0,
                child: Stack(children: [
                  Image.asset(
                    "assets/images/$level",
                  ),
                  Positioned(
                    left: xPosition,
                    top: yPosition,
                    child: Container(
                      width: 50.0 / dpr,
                      height: 50.0 / dpr,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: _newColor,
                      ),
                    ),
                  ),
                ]),
                boundaryMargin: EdgeInsets.all(100),
              ),
            ),
          ],
        ),
      ),
    );
  }

相关问题