首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LateInitializationError:字段'animationController‘即使使用initState()也没有初始化

LateInitializationError:字段'animationController‘即使使用initState()也没有初始化
EN

Stack Overflow用户
提问于 2022-02-02 00:12:22
回答 1查看 286关注 0票数 0

我正在尝试使用SingleTickerProviderStateMixin构建一个登录/注册页面,但是我在AnimationController上发现了错误。我怎么能解决它,甚至尝试使用空?但我得检查一下!在行动中。那时候我也会犯错误。请帮我找出任何解决办法

代码语言:javascript
复制
class log2 extends StatefulWidget {
  const log2({Key? key}) : super(key: key);

  @override
  State<log2> createState() => _log2State();
}

class _log2State extends State<log2> with SingleTickerProviderStateMixin {
  bool islogin = true;
  Animation<double>? containSize;
  late AnimationController animationController;
  Duration duration = Duration(milliseconds: 270);

  @override
  Widget build(BuildContext context) {
    double mobHeight = MediaQuery.of(context).size.height;
    double mobWidth = MediaQuery.of(context).size.width;
    TextEditingController emailController = TextEditingController(),
        passController = TextEditingController();

    initState() {
      super.initState();
      animationController =
          AnimationController(vsync: this, duration: duration);
      SystemChrome.setEnabledSystemUIOverlays([]);
    }

    //For Ticker Screen
    containSize = Tween<double>(begin: mobHeight * 0.1, end: mobWidth * 0.1)
        .animate(
            CurvedAnimation(parent: animationController, curve: Curves.linear));

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

构建Widget:

代码语言:javascript
复制
return SafeArea(
      child: Scaffold(
        body: Stack(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 50),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    "Welcome",
                    style: TextStyle(color: Colors.black, fontSize: 25),
                  ),
                  SizedBox(
                    height: 40,
                  ),
                  Transform.rotate(
                    angle: pi / 4,
                    child: Image.asset(
                      "images/computerdesk.png",
                      fit: BoxFit.cover,
                      height: mobHeight / 3,
                      width: mobWidth / 0.2,
                    ),
                  ),

                  AnimatedBuilder(
                    animation: animationController,
                    builder: (context, child) {
                      return buiderSignupContainer();
                    },
                  ),
                ],
              ),
            ),
            Positioned(
              child: Image.asset("images/ball.png"),
            ),
          ],
        ),
      ),
    );
  }

第二个堆栈小部件:

代码语言:javascript
复制
Widget buiderSignupContainer() {
    double mobheight = MediaQuery.of(context).size.height;
    return Align(
      alignment: Alignment.bottomCenter,
      child: GestureDetector(
        child: Container(
          width: double.infinity,
          height: containSize?.value,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(30),
              topLeft: Radius.circular(30),
            ),
            color: Colors.black.withAlpha(50),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text("Don't Have a Account Yet?"),
              VerticalDivider(width: 5),
              Text(
                "Sign Up",
                style: TextStyle(color: Colors.red),
              ),
            ],
          ),
        ),
        onTap: () {
          setState(() {
            islogin = !islogin;
          });
        },
      ),
    );
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-02 00:44:20

您将initState和dispose方法放置到错误的块。改为:

代码语言:javascript
复制
class _log2State extends State<log2> with SingleTickerProviderStateMixin {   bool islogin = true;   Animation<double>? containSize;   late AnimationController animationController;   Duration duration = Duration(milliseconds: 270);

  bool islogin = true;
  Animation<double>? containSize;
  late AnimationController animationController;
  Duration duration = Duration(milliseconds: 270);

  @override
  void initState() {
     super.initState();
     animationController = AnimationController(vsync: this, duration: duration);
     SystemChrome.setEnabledSystemUIOverlays([]);
     //For Ticker Screen
     containSize = Tween<double>(begin: mobHeight * 0.1, end: mobWidth * 0.1).animate(CurvedAnimation(parent: animationController, curve: Curves.linear));
  }

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

  @override 
  Widget build(BuildContext context) {
    double mobHeight = MediaQuery.of(context).size.height;
    double mobWidth = MediaQuery.of(context).size.width;
    TextEditingController emailController = TextEditingController() passController = TextEditingController();

    return SafeArea(
  child: Scaffold(
    body: Stack(
      children: [
        Padding(
          padding: const EdgeInsets.only(top: 50),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                "Welcome",
                style: TextStyle(color: Colors.black, fontSize: 25),
              ),
              SizedBox(
                height: 40,
              ),
              Transform.rotate(
                angle: pi / 4,
                child: Image.asset(
                  "images/computerdesk.png",
                  fit: BoxFit.cover,
                  height: mobHeight / 3,
                  width: mobWidth / 0.2,
                ),
              ),

              AnimatedBuilder(
                animation: animationController,
                builder: (context, child) {
                  return buiderSignupContainer();
                },
              ),
            ],
          ),
        ),
        Positioned(
          child: Image.asset("images/ball.png"),
        ),
      ],
    ),
  ),
);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70949193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档