setState() не вызывается в statefullWidget в flutter?

#android #ios #flutter

Вопрос:

Здесь я пытаюсь реализовать кнопку для анимации на панели прогресса и следовать этому уроку, но я заметил, что onState это не называется onPressd() ; Вот мой код

 class  ProgressButton extends StatefulWidget {
  @override
  ProgressButtonState createState() => ProgressButtonState();
}

class ProgressButtonState extends State<ProgressButton> with SingleTickerProviderStateMixin {
  bool _isPressed = false;
  int state = 0;
  late Animation _animation;
  GlobalKey globalKey = GlobalKey();
  double width = 80;

  @override
  Widget build(BuildContext context) {
    return PhysicalModel(
        borderRadius: BorderRadius.circular(30.0),
        color: Colors.transparent,
        child: Container(
          width: width,
          child: ElevatedButton(
            key: globalKey,
            onPressed: () {
              _isPressed = true;
              if (state == 0) {
                animateButton();
              }
            },
            child: Text(
              "SignIn",
              style: TextStyle(
                  fontSize: AppFont.appFontSize
              ),
            ),
            style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                primary: AppColors.appFirstColor,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30.0)
                )
            ),
          ),
        )
    );
  }

  void animateButton() {
    double initialWidth = globalKey.currentContext!.size!.width;
    print(initialWidth);
    var controller = AnimationController(duration: Duration(microseconds: 500), vsync: this);
    _animation = Tween(begin: 0.0, end: 1.0)
        .animate(controller)
      ..addListener(() {
        setState(() {
          width = initialWidth-((initialWidth-48.0)*_animation.value);
        });

        controller.forward();
      });
  }
}
 

Комментарии:

1. поместите ispressed в состояние set, тогда он будет работать

2. если я setState() вышел из анимации, чем его работа , но внутри onstate() из aniamtion не работает

3. да, конечно, вы не можете изменить состояние виджета без установленного состояния

4. я хочу анимацию

5. ut нажата в setstate, тогда она будет работать .не рок