如何解决我应该让我的计算函数保持未定义状态吗—不在类中吗?
我在CoverContainer
中放置了一个lib/widgets/cover_container.dart
小部件,需要这些双精度值:
-
coverHeight(context)
-
completeBlur(context)
-
opacity(context)
-
increaseColorLightness(color)
目前,我在这样的单独.dart文件中具有这些功能:
import 'package:flutter/material.dart';
import 'package:chefmenu2/theme/style_constants.dart';
import 'my_scroll_position.dart';
import 'package:provider/provider.dart';
import 'dart:math';
// scroll: ~160
double backLayerAnimationBottomPoint(BuildContext context) =>
MediaQuery.of(context).size.height * kCoverHeightProportion - kBigBoxPadding - bottomTopPointsdiff(context);
// scroll: ~240
double backLayerAnimationTopPoint(BuildContext context) => MediaQuery.of(context).size.height * kCoverHeightProportion - kBigBoxPadding;
// delta btw Top and Bottom points
double bottomTopPointsdiff(BuildContext context) => anchor(context) / 3.5 * MediaQuery.of(context).size.height;
// default cover height before we start scrolling
double defaultCoverHeight(BuildContext context) => (MediaQuery.of(context).size.height) * kCoverHeightProportion + kBigBoxPadding;
// once we reach the bottom point,the cover starts growing according to this formula
double growingCoverHeight(BuildContext context) =>
defaultCoverHeight(context) + Provider.of<MyScrollPosition>(context).data - backLayerAnimationBottomPoint(context);
double coverHeight(BuildContext context) => Provider.of<MyScrollPosition>(context).data < backLayerAnimationBottomPoint(context)
? defaultCoverHeight(context)
: Provider.of<MyScrollPosition>(context).data < MediaQuery.of(context).size.height
? growingCoverHeight(context) // this is the formula
: MediaQuery.of(context).size.height;
double completeBlur(BuildContext context) => Provider.of<MyScrollPosition>(context).data < backLayerAnimationTopPoint(context)
? blur(context,firstPhaseBlur(context))
: blur(context,finalPhaseMaxBlur(context));
double blur(BuildContext context,double getFunction) =>
// handles exception: blur boundary < 0
Provider.of<MyScrollPosition>(context).data < backLayerAnimationBottomPoint(context) ? 0 : getFunction;
double firstPhaseBlur(BuildContext context) =>
pow((Provider.of<MyScrollPosition>(context).data - backLayerAnimationBottomPoint(context)) * kBlurSpeed,kExponent);
double finalPhaseMaxBlur(BuildContext context) => pow(bottomTopPointsdiff(context) * kBlurSpeed,kExponent);
double opacity(BuildContext context) =>
// handles exceptions: opacity boundary < 0 && opacity bouandary > 1
completeOpacity(context) < 0 ? 0 : completeOpacity(context) > 1 ? 1 : completeOpacity(context);
double completeOpacity(BuildContext context) => Provider.of<MyScrollPosition>(context).data < backLayerAnimationTopPoint(context)
? kCoverColorOpacityCoefficient * blur(context,firstPhaseBlur(context))
: kCoverColorOpacityCoefficient * blur(context,finalPhaseMaxBlur(context));
Color increaseColorLightness(Color color) => HSVColor.fromColor(color).withValue(0.9).toColor();
double restaurantTitleTopPadding(BuildContext context) =>
kCoverHeightProportion * MediaQuery.of(context).size.height - kRestaurantTitleMaxShadowBlur * 2;
我的工作方式有效,但是这可能不是最佳方法。根据官方dart guidelines的说法:
” Dart不需要在类内定义所有代码-您可以像使用过程语言或函数语言一样定义顶级变量,常量和函数。 “
另一方面,他们也说:
” 从未实例化的类型是代码气味。但是,这不是硬性规定。 “
正确的方法是什么?这些计算应放在何处以及如何放置(在班级中或现在)?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。