原料按钮onPressed不起作用

时间:2018-09-22 13:17:27

标签: flutter flutter-layout

我正在使用动画生成器来构建布局。我按以下方式在布局中使用“原材料”按钮

      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,
      ),
    ),
  );


}

但是,由于print语句从不打印任何内容,因此不会调用onPressed函数_buttonPressed。 我什至将整个Button代码移到了主要的构建函数中,但仍然无济于事。 我在哪里犯错,请帮忙。 谢谢

2 个答案:

答案 0 :(得分:0)

Viren V mentions in a comment

  

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

对我来说,问题是使用按钮的位置设置为overflow.visible的溢出视图的堆栈视图。将其更改为overflow.hidden,然后将堆栈高度指定为给定值,可以得到所需的设计结果,并且按钮可以再次工作。

答案 1 :(得分:0)

尝试替换:

onPressed: () {
_buttonPressed();
}

由此:

onPressed: _buttonPressed()