【问题标题】:Raw Material Button onPressed not working原材料按钮 onPressed 不起作用
【发布时间】:2018-09-22 13:17:26
【问题描述】:

我正在使用动画构建器来构建布局。我以以下方式在布局中使用 Raw material 按钮

      import 'package:flutter/material.dart';
class MyPage2 extends StatefulWidget {
  @override
  _MyPage2State createState() => _MyPage2State();
}

class _MyPage2State extends State<MyPage2>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        duration: const Duration(milliseconds: 2500),
        vsync: this);
    _controller.forward() ;

    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
      //  _controller.reverse(from:0.80);

      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new MyAnimatedPage(controller:_controller);
  }
}

class MyAnimatedPage extends StatelessWidget {
  MyAnimatedPage({@required AnimationController controller
  }) :animation = new MyPageEnterAnimation(controller);
  final MyPageEnterAnimation animation;

  double sHeight, sWidth;


  void _buttonPressed() {
    print('button pressed');
  }

  Widget _buildAnimation(BuildContext context, Widget child) {
    return
      Column(
        children: <Widget>[
          RawMaterialButton(
            onPressed: () {
              _buttonPressed();
            },
            animationDuration: Duration(milliseconds: 500),
            elevation: 4.0,
            shape: CircleBorder(
                side: BorderSide(width: 4.0, color: Colors.yellow)),
            fillColor: Colors.deepOrangeAccent,
            splashColor: Colors.indigo,
            highlightElevation: 3.0,
            highlightColor: Colors.red,
            padding: EdgeInsets.all(50.0),
            child: Text("+", style: TextStyle(fontSize: 100.0),),
          ),
        ],
      );
  }

  @override
  Widget build(BuildContext context) {
    MediaQueryData data = MediaQuery.of(context);
    sHeight = data.size.height;
    sWidth = data.size.width;

    // TODO: implement build
    return new Scaffold(
      /*  body: new AnimatedBuilder(animation: animation.controller,
          builder: _buildAnimation)*/
      body: Column(
        children: <Widget>[
          RawMaterialButton(
            onPressed: () {
              print('button');
            },
            animationDuration: Duration(milliseconds: 500),
            elevation: 4.0,
            shape: CircleBorder(
                side: BorderSide(width: 4.0, color: Colors.yellow)),
            fillColor: Colors.deepOrangeAccent,
            splashColor: Colors.indigo,
            highlightElevation: 3.0,
            highlightColor: Colors.red,
            padding: EdgeInsets.all(50.0),
            child: Text("+", style: TextStyle(fontSize: 100.0),),
          ),
          Text("Hello Button"),
        ],
      ),
    );
  }
}

class MyPageEnterAnimation {

  final AnimationController controller ;
  final Animation<double> activityOpacity ;
  final Animation<double> logoXTranslation;
  final Animation<double> bannerXTranslation ;
  final Animation<double> sponsorsXTranslation ;

  MyPageEnterAnimation(this.controller) :
        logoXTranslation = new Tween(begin: 0.0, end: 2.75).animate(
          new CurvedAnimation(
            parent: controller,
            curve: new Interval(
              0.200,
              0.500,
              curve: Curves.decelerate,
            ),
          ),
        ),
        activityOpacity = new Tween(begin: 0.0, end: 1.0).animate(

    new CurvedAnimation(
      parent: controller,
      curve: new Interval(
        0.500,
        0.700,
        curve: Curves.ease,
      ),
    ),
  ),
  bannerXTranslation =
  new Tween(begin: 1560.0, end: 0.0).animate(
  new CurvedAnimation(
  parent: controller,
  curve: new Interval(
  0.730,
  0.900,
  curve: Curves.ease,
  ),
  ),
  ),
  sponsorsXTranslation = new Tween(begin: 360.0, end: 50.0).animate(
    new CurvedAnimation(
      parent: controller,
      curve: new Interval(
        0.900,
        1.000,
        curve: Curves.ease,
      ),
    ),
  );


}

然而,onPressed 函数 _buttonPressed 永远不会被调用,因为 print 语句永远不会打印任何东西。 我什至将整个 Button 代码移至主构建函数,但仍然无济于事。 我在哪里可能会犯错误,请帮助。 谢谢

【问题讨论】:

  • “永远不会被调用”是指点击按钮时不会调用它?
  • @GünterZöchbauer 是的,看起来好像函数没有被调用
  • 我只是浏览了你上面的代码,它对我来说工作正常。您是否在任何地方的代码中使用堆栈小部件,那么可能是小部件重叠问题。如果您不使用它,请添加更多详细信息和代码。
  • @VirenVVarasadiya 不,我只是按原样使用上述功能。
  • 你能用你的完整源代码更新这个问题吗?

标签: flutter flutter-layout


【解决方案1】:

尝试替换:

onPressed: () {
_buttonPressed();
}

通过这个:

onPressed: _buttonPressed()

【讨论】:

    【解决方案2】:

    作为Viren V mentions in a comment:

    我只是浏览了您上面的代码,它对我来说工作正常。您是否在任何地方的代码中使用堆栈小部件,那么可能是小部件重叠问题。如果您不使用它,请添加更多详细信息和代码。

    对我来说,问题在于使用堆栈视图,其中溢出设置为 overflow.visible 按钮所在的位置。将其更改为 overflow.hidden 然后给堆栈高度一个给定的值给我想要的设计结果和按钮再次工作。

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 2019-10-30
      • 2019-10-26
      • 2020-04-02
      • 2023-03-07
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      相关资源
      最近更新 更多