【问题标题】:How to add a widget by position on runtime in flutter?如何在颤动中按运行时位置添加小部件?
【发布时间】:2018-11-17 12:48:07
【问题描述】:

我正在尝试在点击位置的图像上添加一个按钮。 我已经设法通过使用 onTapUp 的详细参数来获得点击位置。

但是,当用户点击图片时,我无法添加图标按钮。

下面的代码显示了我的示例。

class ArticlesShowcase extends StatelessWidget {
  final commentWidgets = List<Widget>();
  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
          child: new Center(
            child: Image.network(
              'https://via.placeholder.com/300x500',
            ),
          ),
          onTapUp: (detail) {
            final snackBar = SnackBar(
                content: Text(detail.globalPosition.dx.toString() +
                    " " +
                    detail.globalPosition.dy.toString()));
            Scaffold.of(context).showSnackBar(snackBar);
            new Offset(detail.globalPosition.dx, detail.globalPosition.dy);
            var btn = new RaisedButton(
              onPressed: () => {},
              color: Colors.purple,
              child: new Text(
                "Book",
                style: new TextStyle(color: Colors.white),
              ),
            );
            commentWidgets.add(btn);
          },
        );          
  }
}

我尝试在列表中添加按钮但没有机会。

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    所以,您缺少一些东西。 首先你不能更新StatelessWidget状态,所以你需要使用StatefulWidget

    其次,当使用 StatefulWidget 时,需要调用 setState 来更新 State。 您还需要使用 Stack 和 Positioned 小部件将按钮放在您的特定位置。 您的代码应结束并如下所示。

    class ArticlesShowcaseState extends State<ArticlesShowcase> {
      final commentWidgets = List<Widget>();
      void addButton(detail) {
        {
          final snackBar = SnackBar(
              content: Text(
                  "${detail.globalPosition.dx.toString()} ${detail.globalPosition.dy.toString()}"));
          Scaffold.of(context).showSnackBar(snackBar);
          var btn = new Positioned(
              left: detail.globalPosition.dx,
              top: detail.globalPosition.dy,
              child: RaisedButton(
                onPressed: () => {},
                color: Colors.purple,
                child: new Text(
                  "Book",
                  style: new TextStyle(color: Colors.white),
                ),
              ));
    
          setState(() {
            commentWidgets.add(btn);
          });
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: <Widget>[
                GestureDetector(
                  child: new Center(
                    child: Image.network(
                      'https://via.placeholder.com/300x500',
                    ),
                  ),
                  onTapUp: (detail) => addButton(detail),
                )
              ] +
              commentWidgets,
        );
      }
    }
    

    【讨论】:

    • 谢谢!除了一个功能外,这非常有效。正如我在上面的问题中提到的,我还需要将按钮放在图像上的偏移位置。对此有何评论?
    • 更新了我的答案
    • 非常感谢,您的回答非常清楚,所以我明白了这一点。谢谢!
    猜你喜欢
    • 2019-06-27
    • 2022-07-20
    • 2019-02-13
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2023-03-06
    • 2018-10-18
    • 2021-10-02
    相关资源
    最近更新 更多