首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振Animation<Color>:类'Color‘没有实例方法'-’

颤振Animation<Color>:类'Color‘没有实例方法'-’
EN

Stack Overflow用户
提问于 2020-05-07 08:15:58
回答 1查看 1.1K关注 0票数 3

我制作简单的动画来改变颜色的Appbar使用TweenColor,和Appbar。我希望在滚动屏幕时改变颜色Appbar和Action:

  • 如果用户滚动== minScrollExtent =>,将颜色更改为minScrollExtent,将颜色更改为主颜色。

逻辑

代码语言:javascript
复制
    if (notification.metrics.pixels == notification.metrics.minScrollExtent) {
      Future.delayed(Duration(seconds: 0), () => controllerAppbar.reverse());
    } else {
      Future.delayed(Duration(seconds: 0), () => controllerAppbar.forward());
    }

当我滚动屏幕并达到这两种情况时,我得到了这个错误,但是我得到了我想要的结果。

错误

代码语言:javascript
复制
════════ Exception caught by widgets library ═══════════════════════════════════
Class 'Color' has no instance method '-'.
Receiver: Instance of 'Color'
Tried calling: -(Instance of 'Color')
The relevant error-causing widget was
    AnimatedBuilder 

我怎么能修好它?

AppBarAnimation Widget

代码语言:javascript
复制
class AppBarAnimationColor extends StatefulWidget {

  final Duration duration;
  final AnimationController appBarAnimationController;
  AppBarAnimationColor({
   
    @required this.appBarAnimationController,
    this.duration = const Duration(seconds: 1),
  });
  @override
  AppBarAnimationColorState createState() => AppBarAnimationColorState();
}

class AppBarAnimationColorState extends State<AppBarAnimationColor>
    with SingleTickerProviderStateMixin {
  Animation<Color> appbarColor, iconColor;

  @override
  void initState() {
    super.initState();
    appbarColor = Tween<Color>(begin: Colors.transparent, end: colorPallete.primaryColor)
        .animate(widget.appBarAnimationController);
    iconColor = Tween<Color>(begin: colorPallete.primaryColor, end: colorPallete.white)
        .animate(widget.appBarAnimationController);
  }

  @override
  void dispose() {
    widget.appBarAnimationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: appbarColor,
      builder: (_, __) => AppBar(
        actions: [
          IconButton(
            icon: Icon(FontAwesomeIcons.bars),
            onPressed: () => '',
            color: iconColor.value,
          )
        ],
        elevation: 0,
        backgroundColor: appbarColor.value,
      ),
    );
  }
}

欢迎屏幕

代码语言:javascript
复制
class _WelcomeScreenState extends State<WelcomeScreen>
    with TickerProviderStateMixin {
  AnimationController  _appbarAnimationController;
  @override
  void initState() {
    super.initState();
    _appbarAnimationController =
        AnimationController(vsync: this, duration: kThemeAnimationDuration);
  }

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

  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: (notification) => commonF.handleScrollNotification(
        notification,
        controllerAppbar: _appbarAnimationController,
      ),
      child: SafeArea(
        child: Scaffold(
          extendBodyBehindAppBar: true,
          body: Stack(
            fit: StackFit.expand,
            children: [
              SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    SizedBox(height: 1000),
                  ],
                ),
              ),
             Positioned(
                child: AppBarAnimationColor(
                  appBarAnimationController: _appbarAnimationController,
                ),
                top: 0,
                left: 0,
                right: 0,
              ),
            ],
          )
        ),
      ),
    );
  }
}

逻辑

代码语言:javascript
复制
bool handleScrollNotification(
    ScrollNotification notification, {
    AnimationController controllerAppbar,
  }) {
    
    if (notification.metrics.pixels == notification.metrics.minScrollExtent) {
      Future.delayed(Duration(seconds: 0), () => controllerAppbar.reverse());
    } else {
      Future.delayed(Duration(seconds: 0), () => controllerAppbar.forward());
    }
    return false;
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-07 08:26:17

你用的是吐温,也就是ColorTween。吐温是用于值的变化,如int,float。

代码语言:javascript
复制
appbarColor = ColorTween(begin: Colors.transparent, end: colorPallete.primaryColor)
        .animate(widget.appBarAnimationController);
    iconColor = ColorTween(begin: colorPallete.primaryColor, end: colorPallete.white)
        .animate(widget.appBarAnimationController);
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61652985

复制
相关文章

相似问题

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