【问题标题】:Flutter how to pass back data from model颤振如何从模型传回数据
【发布时间】:2019-01-21 11:07:13
【问题描述】:

我正在练习可拖动的小部件。感谢 Tensor Programming 的精彩视频。我能够理解事情是如何运作的。 但是,有一件事我不确定。假设如果DragBox 之一被点击,我想在被点击的DragBox 内获取文本。我用GestureDetector 包裹了Container,但我不知道如何让onTap 函数将widget.label 传递给AppState

我应该使用构造函数还是其他东西来传递它? 有谁知道要实现这一目标? 提前致谢。

 import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: App(),
      ),
    );
  }
}

class App extends StatefulWidget {
  @override
  AppState createState() => AppState();
}

class AppState extends State<App> {
  Color caughtColor = Colors.grey;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        DragBox(Offset(0.0, 0.0), 'Box One', Colors.blueAccent),
        DragBox(Offset(200.0, 0.0), 'Box Two', Colors.orange),
        DragBox(Offset(300.0, 0.0), 'Box Three', Colors.lightGreen),
        Positioned(
          left: 100.0,
          bottom: 0.0,
          child: DragTarget(
            onAccept: (Color color) {
              caughtColor = color;
            },
            builder: (
              BuildContext context,
              List<dynamic> accepted,
              List<dynamic> rejected,
            ) {
              return Container(
                width: 200.0,
                height: 200.0,
                decoration: BoxDecoration(
                  color: accepted.isEmpty ? caughtColor : Colors.grey.shade200,
                ),
                child: Center(
                  child: Text("Drag Here!"),
                ),
              );
            },
          ),
        )
      ],
    );
  }
}

class DragBox extends StatefulWidget {
  final Offset initPos;
  final String label;
  final Color itemColor;

  DragBox(this.initPos, this.label, this.itemColor);

  @override
  DragBoxState createState() => DragBoxState();
}

class DragBoxState extends State<DragBox> {
  Offset position = Offset(0.0, 0.0);

  @override
  void initState() {
    super.initState();
    position = widget.initPos;
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        left: position.dx,
        top: position.dy,
        child: Draggable(
          data: widget.itemColor,
          child: GestureDetector(
           onTap() {
            // want to pass widget.label here
           },
          child: Container(
            width: 100.0,
            height: 100.0,
            color: widget.itemColor,
            child: Center(
              child: Text(
                widget.label,
                style: TextStyle(
                  color: Colors.white,
                  decoration: TextDecoration.none,
                  fontSize: 20.0,
              ),
             ),
            ),
          ),
         ),
          onDraggableCanceled: (velocity, offset) {
            setState(() {
              position = offset;
            });
          },
          feedback: Container(
            width: 120.0,
            height: 120.0,
            color: widget.itemColor.withOpacity(0.5),
            child: Center(
              child: Text(
                widget.label,
                style: TextStyle(
                  color: Colors.white,
                  decoration: TextDecoration.none,
                  fontSize: 18.0,
                ),
              ),
            ),
          ),
        ));
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您能否参考this的答案。在链接中

    • RootPage 类似于App
    • AppState 类似于_RootPageState
    • DragBox 类似于 FeedPage
    • DragBoxState 类似于 _feedPageState

    如果没有帮助,请发表评论,我可以为您的App举例子

    【讨论】:

    • 非常感谢你让我开心!
    • 抱歉Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.
    • 没关系。谢谢
    • @TwoSan,这很有趣,因为您的声望得分为 419。这会是一个错误吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2022-11-28
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多