【问题标题】:flutter exception: Cannot lerp between "0" and "1"颤振异常:不能在“0”和“1”之间徘徊
【发布时间】:2021-11-14 04:59:08
【问题描述】:

你好吗?我正在学习颤振,我被颤动的兴奋所困扰,我不知道如何解决它。我想为测验 gma 制作一个进度条。我想制作一个 60 秒的进度条,但是当我执行我的代码时,我收到了这个错误:

FlutterError (Cannot lerp between "0" and "1".
The type int returned a double after multiplication with a double value. See "Types with 
special considerations" at https://api.flutter.dev/flutter/animation/Tween-class.html for more 
information.
To lerp int values, consider IntTween or StepTween instead.)

谁能帮帮我??我的 ProgressBar 代码

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:quizz_app/constants.dart';
import 'package:quizz_app/controllers/question_controllers.dart';

class ProgressBar extends StatelessWidget {
   const ProgressBar({
   Key? key,
   }) : super(key: key);

 @override
 Widget build(BuildContext context) {
 return Container(
  width: double.infinity,
  height: 35,
  decoration: BoxDecoration(
      border: Border.all(color: Colors.white.withOpacity(0.2), width: 3),
      borderRadius: BorderRadius.circular(50)),
  child: GetBuilder<QuestionController>(
    init: QuestionController(),
    builder: (controller) {
      print(controller.animation.value);
      return Stack(
        children: [
          LayoutBuilder(
              //layout provide us the available space for the container
              builder: (context, constraints) => Container(
                    width: constraints.maxWidth * 0.5,
                    decoration: BoxDecoration(
                        gradient: kPrimaryGradient,
                        borderRadius: BorderRadius.circular(50)),
                  )),
          Positioned.fill(
              child: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: kDefaultPadding),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              // ignore: prefer_const_literals_to_create_immutables
              children: [
                const Text("Time left:", style: TextStyle(fontSize: 20)),
                const Text(
                  "10",
                  style: TextStyle(fontSize: 20),
                ),
              ],
            ),
          ))
        ],
      );
    },
  ),
);
 }
}

这是我的问题控制器代码,用于控制我的进度条

import 'package:flutter/animation.dart';
import 'package:get/get.dart';

  //we use get pckage for our state management
class QuestionController extends GetxController
  with SingleGetTickerProviderMixin {
 late AnimationController _animationController;
 late Animation _animation;
 Animation get animation => this._animation;

 @override
 void onInit() {
 _animationController =
    AnimationController(duration: const Duration(seconds: 60), vsync: this);
_animation = Tween(begin: 0, end: 1).animate(_animationController)
  ..addListener(() {
    //update like setstate
    update();
  });
_animationController.forward();
super.onInit();
 }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    Tween(begin: 0, end: 1) 更改为 Tween(begin: 0.0, end: 1.0)

    【讨论】:

    • 标记为真以便其他人可以看到解决方案
    【解决方案2】:

    Tween 在两个值之间进行插值。它是一个 generic 类型;如果您没有明确指定类型参数,它将从构造参数中推断出来。由于您使用了Tween(begin: 0, end: 1),因此从01 整数文字推断出具体类型为Tween&lt;int&gt;。您不能使用ints 在01 之间进行插值,因此您必须使用double 文字,以便推断类型为Tween&lt;double&gt;

    Tween(begin: 0.0, end: 0.0)
    

    或明确请求Tween&lt;double&gt;

    Tween<double>(begin: 0, end: 0)
    

    【讨论】:

      【解决方案3】:

      如果是 int,您应该使用 IntTween,而不是常规 Tween。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-10
        • 1970-01-01
        相关资源
        最近更新 更多