Можно ли иметь два AnimatedBuilder в двух разных классах? - PullRequest
0 голосов
/ 07 февраля 2020

Я пытался создать действительно базовый c таймер, используя AnimationController и AnimatedBuilder.

Имя ошибки: 'исключено 10 кадров из класса _AssertionError, пакет dart: asyn c и пакет dart: asyn c -patch '

Мой проект требует два AnimatedBuilder, они работают, если я помещаю их в один класс.

К сожалению, моему проекту нужны два отдельных класса для этого.

Что я могу сделать?

Кстати, это не мой проект, я просто сократил его, чтобы вам было легче помочь мне ответить на мой вопрос.

    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }

    class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
      AnimationController animationController;

      String get timerString {
        Duration duration =
            animationController.duration * animationController.value;
        return '${duration.inSeconds}';
      }

      @override
      void initState() {
        super.initState();
        animationController = AnimationController(
            vsync: this, duration: Duration(seconds: 10), value: 1.0);
      }

      void startAnimating() async {
        await animationController.reverse();
      }

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              leading: AnimatedBuilder(
                animation: animationController,
                builder: (BuildContext context, Widget child) {
                  return IconContainer();
                },
              ),
            ),
            backgroundColor: Colors.green,
            body: Column(
              children: <Widget>[
                Center(
                    child: AnimatedBuilder(
                  animation: animationController,
                  builder: (BuildContext context, Widget child) {
                    return Text(
                      '$timerString',
                      style: TextStyle(color: Colors.black, fontSize: 50),
                    );
                  },
                ))
              ],
            ),
          ),
        );
      }
    }

    final myApp = _MyAppState();

    class IconContainer extends StatefulWidget {
      @override
      _IconContainerState createState() => _IconContainerState();
    }

    class _IconContainerState extends State<IconContainer> {
      @override
      void initState() {
        super.initState();
      }

      @override
      Widget build(BuildContext context) {
        return AnimatedBuilder(
          animation: myApp.animationController,
          builder: (BuildContext context, Widget child) {
            return IconButton(
                icon: Icon(myApp.animationController.isAnimating
                    ? Icons.pause
                    : Icons.play_arrow),
                onPressed: () {
                  myApp.startAnimating();
                });
          },
        );
      }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...