【问题标题】:The method 'findAncestorStateOfType' was called on null. Receiver: null Tried calling: findAncestorStateOfType<NavigatorState>() error在 null 上调用了方法“findAncestorStateOfType”。接收方:null 尝试调用:findAncestorStateOfType<NavigatorState>() 错误
【发布时间】:2021-08-08 07:21:22
【问题描述】:

谁能帮我解决这个问题?我正在关注有关动画径向菜单的网站编码教程。但是本教程没有显示如何通过按下 FloatingActionButton 导航到其他页面。因此,我自己尝试了,但出现了这个错误。

这是我的代码

class RadialAnimation extends StatelessWidget { 
final AnimationController controller;
RadialAnimation({Key key, this.controller})
  : scale = Tween<double>(
      begin: 1.5,
      end: 0.0,
    ).animate(
      CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn),
    ),
    translation = Tween<double>(
      begin: 0.0,
      end: 100.0,
    ).animate(
      CurvedAnimation(
        parent: controller,
        curve: Curves.elasticOut,
      ),
    ),
    rotation = Tween<double>(
      begin: 0.0,
      end: 360.0,
    ).animate(
      CurvedAnimation(
        parent: controller,
        curve: Interval(
          0.0,
          0.7,
        ),
      ),
    ),
    super(key: key);

// final AnimationController controller;
final Animation<double> scale;
final Animation<double> translation;
final Animation<double> rotation;

build(context) {
return AnimatedBuilder(
    animation: controller,
    builder: (context, builder) {
      return Transform.rotate(
        angle: radians(rotation.value),
        child: Stack(
          alignment: Alignment.center,
          children: [
            _buildButton1(45,
                color: Colors.red, icon: FontAwesomeIcons.thumbtack),
            _buildButton2(180,
                color: Colors.red, icon: FontAwesomeIcons.fire),
            _buildButton3(315,
                color: Colors.red, icon: FontAwesomeIcons.bolt),

            Transform.scale(
              scale: scale.value - 1,
              child: FloatingActionButton(
                child: Icon(FontAwesomeIcons.timesCircle),
                onPressed: _close,
              ),
            ),
            
            Transform.scale(
              scale: scale.value,
              child: FloatingActionButton(
                child: Icon(FontAwesomeIcons.timesCircle),
                onPressed: _open,
              ),
            ),
          ],
        ),
      );
    });
}

这是我尝试修改代码的地方。

_buildButton1(double angle, {Color color, IconData icon}) {
final double rad = radians(angle);
return Transform(
    transform: Matrix4.identity()
      ..translate(
        (translation.value) * cos(rad),
        (translation.value) * sin(rad),
      ),
    child: FloatingActionButton(
      child: Icon(icon),
      backgroundColor: color,
      onPressed: () {
        BuildContext context;
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => BT()),
        );
      },
    ));
   }

【问题讨论】:

    标签: flutter dart floating-action-button nosuchmethoderror


    【解决方案1】:

    每次构建新按钮时,传递一个context。无论您走到哪里,始终保持context 的安全。

    // Add a context as a parameter. Keep it safe.
    _buildButton1(BuildContext context, double angle, {Color color, IconData icon}) {
      return Transform(
        # ...
        child: FloatingActionButton(
          child: Icon(icon),
          backgroundColor: color,
          onPressed: () {
            // No, that's wrong. Comment it, or even delete it FOREVER
            // BuildContext context;
            Navigator.push(context, MaterialPageRoute(builder: (context) => BT()));
          },
        ),
      );
    }
    

    当您调用_buildButton1 时,只需传递上下文。保持安全。

    build(context) {
      return AnimatedBuilder(
        animation: controller,
        builder: (context, builder) {
          // Here's your context. Use it. Keep it safe.
          return Transform.rotate(
            angle: radians(rotation.value),
            child: Stack(
              alignment: Alignment.center,
              children: [
                // Use it. Keep it safe.
                _buildButton1(context, 45,
                    color: Colors.red, icon: FontAwesomeIcons.thumbtack),
                _buildButton2(context, 180,
                    color: Colors.red, icon: FontAwesomeIcons.fire),
                _buildButton3(context, 315,
                    color: Colors.red, icon: FontAwesomeIcons.bolt),
    
                Transform.scale(
                  scale: scale.value - 1,
                  child: FloatingActionButton(
                    child: Icon(FontAwesomeIcons.timesCircle),
                    onPressed: _close,
                  ),
                ),
                
                Transform.scale(
                  scale: scale.value,
                  child: FloatingActionButton(
                    child: Icon(FontAwesomeIcons.timesCircle),
                    onPressed: _open,
                  ),
                ),
              ],
            ),
          );
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2022-01-22
      • 2021-08-26
      • 2020-12-06
      • 2021-01-14
      • 2020-05-24
      • 2022-08-05
      相关资源
      最近更新 更多