Flutter - 从自定义小部件返回onTap并执行动画

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

我有一个名为RowItem的自定义Widget,它是一个有状态的小部件。当用户单击它动画的小部件时。但是我也希望将onTap事件返回到父窗口小部件,但我不知道如何做到这一点......

这是我的RowItem小部件:

class _RowItemState extends State<RowItem> {
  bool isSelected = false;
  double underlineWith = 0;

  @override
  Widget build(context) {
    // Pass the text down to another widget
    return new GestureDetector(
      onTap: () {
        isSelected = !isSelected;
        if (isSelected) {
          setState(() {
            underlineWith = 18.0;
          });
        } else {
          setState(() {
            underlineWith = 0;
          });
        }
      },
      child: new Column(
        children: <Widget>[
          new Padding(
              padding: EdgeInsetsDirectional.fromSTEB(8.0, 0.0, 8.0, 0.0),
              child: new Container(
                child: new Text(
                  widget.text,
                  textAlign: TextAlign.center,
                  overflow: TextOverflow.ellipsis,
                  softWrap: true,
                  style: TextStyle(
                      fontSize: 12.0,
                      fontWeight: FontWeight.bold,
                      color: Color(0x99222222)),
                ),
              )),
          new Padding(
            padding: EdgeInsetsDirectional.fromSTEB(0.0, 5.0, 0.0, 10.0),
            child: new AnimatedContainer(
              height: 2,
              width: underlineWith,
              color: widget.underlineColor,
              duration: Duration(milliseconds: 150),
            ),
          ),
        ],
      ),
    );
  }
}

如果我想返回onTap事件,我可以这样做:

return new GestureDetector(
  onTap: widget.onTap
}

但我如何同时做两个动画并返回onTap事件?

我想在点击RowItem时运行代码:

new GridView.builder(
                                  physics: const NeverScrollableScrollPhysics(),
                                  shrinkWrap: true,
                                  itemCount: _subCategories.length,
                                  gridDelegate:
                                  new SliverGridDelegateWithFixedCrossAxisCount(
                                      childAspectRatio: 3.2,
                                      crossAxisCount: 3),
                                  itemBuilder: (BuildContext context, int index) {
                                    return new RowItem(
                                      text: _subCategories[index],
                                      underlineColor: Color(0xff73A6AD),
                                      onTap: () {
                                        setState(() {
                                          //do something;
                                        });
                                      },
                                    );
                                  }
)
flutter flutter-animation
2个回答
1
投票

在RowItem里面创建一个函数handleOnTap()。在此函数中,您更新您的值和setState然后调用widget.onTap()

func handleOnTap() {
  isSelected = !isSelected;
  if (isSelected) {
    setState(() {
      underlineWith = 18.0;
    });
  } else {
    setState(() {
      underlineWith = 0;
    });
  }
  widget.onTap();
}

1
投票

找到了基于侯赛因答案的解决方案:

handleOnTap(){
    isSelected = !isSelected;
    if (isSelected) {
      setState(() {
        underlineWith = 18.0;
      });
    } else {
      setState(() {
        underlineWith = 0;
      });
    }
    return widget.onTap();
}





@override
Widget build(context) {
    // Pass the text down to another widget
    return new GestureDetector(
      onTap: () => handleOnTap()
      ...
© www.soinside.com 2019 - 2024. All rights reserved.