可拖动小部件在拖动时丢失文本样式 - Flutter

问题描述 投票:0回答:1

当我拖动视图时,文本会更改其样式。我猜想正在进行一些重建工作。知道如何使文本在拖动时保持不变吗?

Positioned(
    left: _textPosition.dx,
    top: _textPosition.dy,
    child: Draggable(
      feedback: Container(
        color: black_transparent_light,
        child:  Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            "Some Text Here",
            style: const TextStyle( // Preserve original style
                color: Colors.white, // Adjust as needed
                fontSize: 16, // Adjust as needed
                fontStyle: FontStyle.normal,
                decoration: TextDecoration.none
            ),
          ),
        )
      ),
      childWhenDragging: Container(),
      onDragEnd: (details)
      {
        final RenderBox renderBox = _mainContainerKey.currentContext!.findRenderObject() as RenderBox;
        final Offset position = renderBox.localToGlobal(Offset.zero); // This is the absolute position

        setState(() {
          _textPosition = Offset(
              details.offset.dx-position.dx - mainContainerStartMargin,
              details.offset.dy-position.dy-mainContainerTopMargin);
        });
      },
      child: Container(
        color: black_transparent_light,
        child:  Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            "Some Text Here",
            style: const TextStyle( // Preserve original style
                color: Colors.white, // Adjust as needed
                fontSize: 16, // Adjust as needed
                fontStyle: FontStyle.normal,
                decoration: TextDecoration.none
            ),
          ),
        ),
      ),
    ),
  ),
flutter draggable
1个回答
0
投票

要解决此问题,我所要做的就是使用 Material Widget 将容器包装在反馈中:

feedback: Material(
   color: Colors.transparent,
   child: Container(
   color: black_transparent_light,
   child:  Padding(
   padding: const EdgeInsets.all(8.0),
     child: Text(
             "Some Text Here",
             style: const TextStyle( // Preserve original style
              color: Colors.white, // Adjust as needed
             fontSize: 16, // Adjust as needed
                fontStyle: FontStyle.normal,
               decoration: TextDecoration.none
                 ),
               ),
            )
      ),
)
© www.soinside.com 2019 - 2024. All rights reserved.