Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ 0024 [对象] 一个对象有很多属性时,如何便捷地复制出另一个略有差异对象 #58

Open
toly1994328 opened this issue Sep 11, 2020 · 1 comment
Labels
point The point of Flutter

Comments

@toly1994328
Copy link
Owner

可以参考Flutter源码,在类中给出一个copyWith的方法。(当然,方法名可以随意)

@toly1994328
Copy link
Owner Author

比如MediaQueryData有非常多的属性,如果根据旧对象想得到只是一两条不同的新对象,一点点写会比较麻烦.

  const MediaQueryData({
    this.size = Size.zero,
    this.devicePixelRatio = 1.0,
    this.textScaleFactor = 1.0,
    this.platformBrightness = Brightness.light,
    this.padding = EdgeInsets.zero,
    this.viewInsets = EdgeInsets.zero,
    this.systemGestureInsets = EdgeInsets.zero,
    this.viewPadding = EdgeInsets.zero,
    this.alwaysUse24HourFormat = false,
    this.accessibleNavigation = false,
    this.invertColors = false,
    this.highContrast = false,
    this.disableAnimations = false,
    this.boldText = false,
    this.navigationMode = NavigationMode.traditional,
  })

MediaQueryData源码中使用copyWith方便构建新对象。

MediaQueryData copyWith({
  Size size,
  double devicePixelRatio,
  double textScaleFactor,
  Brightness platformBrightness,
  EdgeInsets padding,
  EdgeInsets viewPadding,
  EdgeInsets viewInsets,
  EdgeInsets systemGestureInsets,
  bool alwaysUse24HourFormat,
  bool highContrast,
  bool disableAnimations,
  bool invertColors,
  bool accessibleNavigation,
  bool boldText,
  NavigationMode navigationMode,
}) {
  return MediaQueryData(
    size: size ?? this.size,
    devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
    textScaleFactor: textScaleFactor ?? this.textScaleFactor,
    platformBrightness: platformBrightness ?? this.platformBrightness,
    padding: padding ?? this.padding,
    viewPadding: viewPadding ?? this.viewPadding,
    viewInsets: viewInsets ?? this.viewInsets,
    systemGestureInsets: systemGestureInsets ?? this.systemGestureInsets,
    alwaysUse24HourFormat: alwaysUse24HourFormat ?? this.alwaysUse24HourFormat,
    invertColors: invertColors ?? this.invertColors,
    highContrast: highContrast ?? this.highContrast,
    disableAnimations: disableAnimations ?? this.disableAnimations,
    accessibleNavigation: accessibleNavigation ?? this.accessibleNavigation,
    boldText: boldText ?? this.boldText,
    navigationMode: navigationMode ?? this.navigationMode,
  );
}

@toly1994328 toly1994328 added the point The point of Flutter label Sep 11, 2020
@toly1994328 toly1994328 changed the title + 024 一个对象有很多属性时,如何便捷地复制出另一个略有差异对象 + 0024 一个对象有很多属性时,如何便捷地复制出另一个略有差异对象 Sep 11, 2020
@toly1994328 toly1994328 changed the title + 0024 一个对象有很多属性时,如何便捷地复制出另一个略有差异对象 + 0024 [对象] 一个对象有很多属性时,如何便捷地复制出另一个略有差异对象 Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
point The point of Flutter
Projects
None yet
Development

No branches or pull requests

1 participant