浏览代码

upgrade switch widgets

/siyaoH-1.17-PlatformMessage
xingweizhu 4 年前
当前提交
35f2f759
共有 2 个文件被更改,包括 463 次插入65 次删除
  1. 341
      com.unity.uiwidgets/Runtime/material/switch.cs
  2. 187
      com.unity.uiwidgets/Runtime/material/swtich_list_tile.cs

341
com.unity.uiwidgets/Runtime/material/switch.cs


using System.Collections.Generic;
using Unity.UIWidgets.scheduler2;
using Unity.UIWidgets.service;
using ImageUtils = Unity.UIWidgets.widgets.ImageUtils;
namespace Unity.UIWidgets.material {
enum _SwitchType {

Color inactiveThumbColor = null,
Color inactiveTrackColor = null,
ImageProvider activeThumbImage = null,
ImageErrorListener onActiveThumbImageError = null,
ImageErrorListener onInactiveThumbImageError = null,
DragStartBehavior dragStartBehavior = DragStartBehavior.start
DragStartBehavior dragStartBehavior = DragStartBehavior.start,
Color focusColor = null,
Color hoverColor = null,
FocusNode focusNode = null,
bool autofocus = false
) : this(
key: key,
value: value,

inactiveThumbColor: inactiveThumbColor,
inactiveTrackColor: inactiveTrackColor,
activeThumbImage: activeThumbImage,
onActiveThumbImageError: onActiveThumbImageError,
onInactiveThumbImageError: onInactiveThumbImageError,
dragStartBehavior: dragStartBehavior
dragStartBehavior: dragStartBehavior,
focusColor: focusColor,
hoverColor: hoverColor,
focusNode: focusNode,
autofocus: autofocus
) {
}

Color inactiveThumbColor = null,
Color inactiveTrackColor = null,
ImageProvider activeThumbImage = null,
ImageErrorListener onActiveThumbImageError = null,
ImageErrorListener onInactiveThumbImageError = null,
DragStartBehavior dragStartBehavior = DragStartBehavior.start
DragStartBehavior dragStartBehavior = DragStartBehavior.start,
Color focusColor = null,
Color hoverColor = null,
FocusNode focusNode = null,
bool autofocus = false
D.assert(activeThumbImage != null || onActiveThumbImageError == null);
D.assert(inactiveThumbImage != null || onInactiveThumbImageError == null);
this.onChanged = onChanged;
this.activeColor = activeColor;
this.activeTrackColor = activeTrackColor;

this.onActiveThumbImageError = onActiveThumbImageError;
this.onInactiveThumbImageError = onInactiveThumbImageError;
this.focusColor = focusColor;
this.hoverColor = hoverColor;
this.focusNode = focusNode;
this.autofocus = autofocus;
}
public static Switch adaptive(

Color inactiveThumbColor = null,
Color inactiveTrackColor = null,
ImageProvider activeThumbImage = null,
ImageErrorListener onActiveThumbImageError = null,
ImageErrorListener onInactiveThumbImageError = null,
DragStartBehavior dragStartBehavior = DragStartBehavior.down
DragStartBehavior dragStartBehavior = DragStartBehavior.start,
Color focusColor = null,
Color hoverColor = null,
FocusNode focusNode = null,
bool autofocus = false
) {
return new Switch(key: key,
value: value,

inactiveThumbColor: inactiveThumbColor,
inactiveTrackColor: inactiveTrackColor,
activeThumbImage: activeThumbImage,
onActiveThumbImageError: onActiveThumbImageError,
onInactiveThumbImageError: onInactiveThumbImageError,
switchType: _SwitchType.adaptive
switchType: _SwitchType.adaptive,
dragStartBehavior: dragStartBehavior,
focusColor: focusColor,
hoverColor: hoverColor,
focusNode: focusNode,
autofocus: autofocus
);
}

public readonly ImageProvider activeThumbImage;
public readonly ImageErrorListener onActiveThumbImageError;
public readonly ImageErrorListener onInactiveThumbImageError;
public readonly MaterialTapTargetSize? materialTapTargetSize;

public readonly Color focusColor;
public readonly Color hoverColor;
public readonly FocusNode focusNode;
public readonly bool autofocus;
public override State createState() {
return new _SwitchState();

}
class _SwitchState : TickerProviderStateMixin<Switch> {
Dictionary<LocalKey, ActionFactory> _actionMap;
public override void initState() {
base.initState();
_actionMap = new Dictionary<LocalKey, ActionFactory>();
_actionMap[ActivateAction.key] = _createAction;
}
void _actionHandler(FocusNode node, Intent intent) {
if (widget.onChanged != null) {
widget.onChanged(!widget.value);
}
RenderObject renderObject = node.context.findRenderObject();
}
UiWidgetAction _createAction() {
return new CallbackAction(
ActivateAction.key,
onInvoke: _actionHandler
);
}
bool _focused = false;
void _handleFocusHighlightChanged(bool focused) {
if (focused != _focused) {
setState(() => { _focused = focused; });
}
}
bool _hovering = false;
void _handleHoverChanged(bool hovering) {
if (hovering != _hovering) {
setState(() => { _hovering = hovering; });
}
}
Size getSwitchSize(ThemeData theme) {
switch (widget.materialTapTargetSize ?? theme.materialTapTargetSize) {
case MaterialTapTargetSize.padded:

return null;
}
bool enabled {
get { return widget.onChanged != null; }
}
internal void _didFinishDragging() {
setState(() => { });
}
Widget buildMaterialSwitch(BuildContext context) {
D.assert(material_.debugCheckHasMaterial(context));
ThemeData theme = Theme.of(context);

Color activeTrackColor = widget.activeTrackColor ?? activeThumbColor.withAlpha(0x80);
Color hoverColor = widget.hoverColor ?? theme.hoverColor;
Color focusColor = widget.focusColor ?? theme.focusColor;
if (widget.onChanged != null) {
Color black32 = new Color(0x52000000); // Black with 32% opacity
if (enabled) {
Color black32 = new Color(0x52000000);
inactiveThumbColor = widget.inactiveThumbColor ??
(isDark ? Colors.grey.shade400 : Colors.grey.shade50);
inactiveTrackColor = widget.inactiveTrackColor ?? (isDark ? Colors.white30 : black32);

inactiveTrackColor = widget.inactiveTrackColor ?? (isDark ? Colors.white10 : Colors.black12);
}
return new _SwitchRenderObjectWidget(
dragStartBehavior: widget.dragStartBehavior,
value: widget.value,
activeColor: activeThumbColor,
inactiveColor: inactiveThumbColor,
activeThumbImage: widget.activeThumbImage,
inactiveThumbImage: widget.inactiveThumbImage,
activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor,
configuration: ImageUtils.createLocalImageConfiguration(context),
onChanged: widget.onChanged,
additionalConstraints: BoxConstraints.tight(getSwitchSize(theme)),
vsync: this
return new FocusableActionDetector(
actions: _actionMap,
focusNode: widget.focusNode,
autofocus: widget.autofocus,
enabled: enabled,
onShowFocusHighlight: _handleFocusHighlightChanged,
onShowHoverHighlight: _handleHoverChanged,
child: new Builder(
builder: (BuildContext context) => {
return new _SwitchRenderObjectWidget(
dragStartBehavior: widget.dragStartBehavior,
value: widget.value,
activeColor: activeThumbColor,
inactiveColor: inactiveThumbColor,
hoverColor: hoverColor,
focusColor: focusColor,
activeThumbImage: widget.activeThumbImage,
onActiveThumbImageError: widget.onActiveThumbImageError,
inactiveThumbImage: widget.inactiveThumbImage,
onInactiveThumbImageError: widget.onInactiveThumbImageError,
activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor,
configuration: ImageUtils.createLocalImageConfiguration(context),
onChanged: widget.onChanged,
additionalConstraints: BoxConstraints.tight(getSwitchSize(theme)),
hasFocus: _focused,
hovering: _hovering,
state: this
);
}
)
// Widget buildCupertinoSwitch(BuildContext context) {
// Size size = this.getSwitchSize(Theme.of(context));
// return new Container(
// width: size.width, // Same size as the Material switch.
// height: size.height,
// alignment: Alignment.center,
// child: CupertinoSwitch(
// value: this.widget.value,
// onChanged: this.widget.onChanged,
// activeColor: this.widget.activeColor
// )
// );
// }
// ThemeData theme = Theme.of(context);
// D.assert(theme.platform != null);
// switch (theme.platform) {
// case TargetPlatform.android:
// return buildMaterialSwitch(context);
// case TargetPlatform.iOS:
// return buildCupertinoSwitch(context);
// }
// break;
}
}

bool? value = null,
Color activeColor = null,
Color inactiveColor = null,
Color hoverColor = null,
Color focusColor = null,
ImageErrorListener onActiveThumbImageError = null,
ImageErrorListener onInactiveThumbImageError = null,
TickerProvider vsync = null,
DragStartBehavior? dragStartBehavior = null
DragStartBehavior? dragStartBehavior = null,
bool hasFocus = false,
bool hovering = false,
_SwitchState state = null
this.hoverColor = hoverColor;
this.focusColor = focusColor;
this.onActiveThumbImageError = onActiveThumbImageError;
this.onInactiveThumbImageError = onInactiveThumbImageError;
this.vsync = vsync;
this.hasFocus = hasFocus;
this.hovering = hovering;
this.state = state;
public readonly Color hoverColor;
public readonly Color focusColor;
public readonly ImageErrorListener onActiveThumbImageError;
public readonly ImageErrorListener onInactiveThumbImageError;
public readonly TickerProvider vsync;
public readonly bool hasFocus;
public readonly bool hovering;
public readonly _SwitchState state;
public override RenderObject createRenderObject(BuildContext context) {
return new _RenderSwitch(

inactiveColor: inactiveColor,
hoverColor: hoverColor,
focusColor: focusColor,
onActiveThumbImageError: onActiveThumbImageError,
onInactiveThumbImageError: onInactiveThumbImageError,
vsync: vsync
textDirection: Directionality.of(context),
hasFocus: hasFocus,
hovering: hovering,
state: state
);
}

renderObject.value = value;
renderObject.activeColor = activeColor;
renderObject.inactiveColor = inactiveColor;
renderObject.hoverColor = hoverColor;
renderObject.focusColor = focusColor;
renderObject.onActiveThumbImageError = onActiveThumbImageError;
renderObject.onInactiveThumbImageError = onInactiveThumbImageError;
renderObject.textDirection = Directionality.of(context);
renderObject.vsync = vsync;
renderObject.hasFocus = hasFocus;
renderObject.hovering = hovering;
renderObject.vsync = state;
}
}

Color activeColor = null,
Color inactiveColor = null,
Color hoverColor = null,
Color focusColor = null,
ImageErrorListener onActiveThumbImageError = null,
ImageErrorListener onInactiveThumbImageError = null,
TextDirection? textDirection = null,
TickerProvider vsync = null,
DragStartBehavior? dragStartBehavior = null
DragStartBehavior? dragStartBehavior = null,
bool hasFocus = false,
bool hovering = false,
_SwitchState state = null
hoverColor: hoverColor,
focusColor: focusColor,
vsync: vsync
hasFocus: hasFocus,
hovering: hovering,
vsync: state
D.assert(textDirection != null);
_onActiveThumbImageError = onActiveThumbImageError;
_onInactiveThumbImageError = onInactiveThumbImageError;
_activeTrackColor = activeTrackColor;
_inactiveTrackColor = inactiveTrackColor;
_configuration = configuration;

ImageProvider _activeThumbImage;
public ImageErrorListener onActiveThumbImageError {
get { return _onActiveThumbImageError; }
set {
if (value == _onActiveThumbImageError) {
return;
}
_onActiveThumbImageError = value;
markNeedsPaint();
}
}
ImageErrorListener _onActiveThumbImageError;
public ImageProvider inactiveThumbImage {
get { return _inactiveThumbImage; }
set {

}
ImageProvider _inactiveThumbImage;
public ImageErrorListener onInactiveThumbImageError {
get { return _onInactiveThumbImageError; }
set {
if (value == _onInactiveThumbImageError) {
return;
}
_onInactiveThumbImageError = value;
markNeedsPaint();
}
}
ImageErrorListener _onInactiveThumbImageError;
public Color activeTrackColor {
get { return _activeTrackColor; }

ImageConfiguration _configuration;
public TextDirection textDirection {
get { return _textDirection; }
set {
if (_textDirection == value) {
return;
}
_textDirection = value;
markNeedsPaint();
}
}
TextDirection _textDirection;
_SwitchState state;
public override bool? value {
get { return base.value; }
set {
D.assert(value != null);
base.value = value;
if (_needsPositionAnimation) {
_needsPositionAnimation = false;
position.curve = null;
position.reverseCurve = null;
if (value == true) {
positionController.forward();
}
else {
positionController.reverse();
}
}
}
}
public override void detach() {
_cachedThumbPainter?.Dispose();

}
HorizontalDragGestureRecognizer _drag;
bool _needsPositionAnimation = false;
void _handleDragStart(DragStartDetails details) {
if (isInteractive) {

}
void _handleDragEnd(DragEndDetails details) {
if (position.value >= 0.5) {
positionController.forward();
}
else {
positionController.reverse();
_needsPositionAnimation = true;
if ((position.value >= 0.5f) != value) {
onChanged(!value);
state._didFinishDragging();
}
public override void handleEvent(PointerEvent evt, HitTestEntry entry) {

Color _cachedThumbColor;
ImageProvider _cachedThumbImage;
ImageErrorListener _cachedThumbErrorListener;
BoxDecoration _createDefaultThumbDecoration(Color color, ImageProvider image) {
BoxDecoration _createDefaultThumbDecoration(Color color, ImageProvider image,
ImageErrorListener errorListener) {
image: image == null ? null : new DecorationImage(image: image),
image: image == null ? null : new DecorationImage(image: image, onError: errorListener),
shape: BoxShape.circle,
boxShadow: ShadowConstants.kElevationToShadow[1]
);

? (currentValue < 0.5 ? inactiveThumbImage : activeThumbImage)
: inactiveThumbImage;
ImageErrorListener thumbErrorListener = isEnabled
? (currentValue < 0.5f ? onInactiveThumbImageError : onActiveThumbImageError)
: onInactiveThumbImageError;
// Paint the track
Paint paint = new Paint {color = trackColor};
float trackHorizontalPadding = Constants.kRadialReactionRadius - Switch._kTrackRadius;

_isPainting = true;
BoxPainter thumbPainter;
if (_cachedThumbPainter == null || thumbColor != _cachedThumbColor ||
thumbImage != _cachedThumbImage) {
thumbImage != _cachedThumbImage || thumbErrorListener != _cachedThumbErrorListener) {
_cachedThumbPainter = _createDefaultThumbDecoration(thumbColor, thumbImage)
_cachedThumbErrorListener = thumbErrorListener;
_cachedThumbPainter = _createDefaultThumbDecoration(thumbColor, thumbImage, thumbErrorListener)
.createBoxPainter(_handleDecorationChanged);
}

187
com.unity.uiwidgets/Runtime/material/swtich_list_tile.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
namespace Unity.UIWidgets.material {
public enum _SwitchListTileType {
material,
adaptive
}
public class SwitchListTile : StatelessWidget {
public SwitchListTile(
Key key = null,
bool? value = null,
ValueChanged<bool?> onChanged = null,
Color activeColor = null,
Color activeTrackColor = null,
Color inactiveThumbColor = null,
Color inactiveTrackColor = null,
ImageProvider activeThumbImage = null,
ImageProvider inactiveThumbImage = null,
Widget title = null,
Widget subtitle = null,
bool isThreeLine = false,
bool? dense = null,
EdgeInsets contentPadding = null,
Widget secondary = null,
bool selected = false,
_SwitchListTileType _switchListTileType = _SwitchListTileType.material
) : base(key: key) {
D.assert(value != null);
D.assert(!isThreeLine || subtitle != null);
this.value = value.Value;
this.onChanged = onChanged;
this.activeColor = activeColor;
this.activeTrackColor = activeTrackColor;
this.inactiveThumbColor = inactiveThumbColor;
this.inactiveTrackColor = inactiveTrackColor;
this.activeThumbImage = activeThumbImage;
this.inactiveThumbImage = inactiveThumbImage;
this.title = title;
this.subtitle = subtitle;
this.isThreeLine = isThreeLine;
this.dense = dense;
this.contentPadding = contentPadding;
this.secondary = secondary;
this.selected = selected;
this._switchListTileType = _switchListTileType;
}
public static SwitchListTile adaptive(
Key key = null,
bool? value = null,
ValueChanged<bool?> onChanged = null,
Color activeColor = null,
Color activeTrackColor = null,
Color inactiveThumbColor = null,
Color inactiveTrackColor = null,
ImageProvider activeThumbImage = null,
ImageProvider inactiveThumbImage = null,
Widget title = null,
Widget subtitle = null,
bool isThreeLine = false,
bool? dense = null,
EdgeInsets contentPadding = null,
Widget secondary = null,
bool selected = false) {
return new SwitchListTile(
key: key,
value: value,
onChanged: onChanged,
activeColor: activeColor,
activeTrackColor: activeTrackColor,
inactiveThumbColor: inactiveThumbColor,
inactiveTrackColor: inactiveTrackColor,
activeThumbImage: activeThumbImage,
inactiveThumbImage: inactiveThumbImage,
title: title,
subtitle: subtitle,
isThreeLine: isThreeLine,
dense: dense,
contentPadding: contentPadding,
secondary: secondary,
selected: selected,
_switchListTileType: _SwitchListTileType.adaptive
);
}
public readonly bool value;
public readonly ValueChanged<bool?> onChanged;
public readonly Color activeColor;
public readonly Color activeTrackColor;
public readonly Color inactiveThumbColor;
public readonly Color inactiveTrackColor;
public readonly ImageProvider activeThumbImage;
public readonly ImageProvider inactiveThumbImage;
public readonly Widget title;
public readonly Widget subtitle;
public readonly Widget secondary;
public readonly bool isThreeLine;
public readonly bool? dense;
public readonly EdgeInsets contentPadding;
public readonly bool selected;
public readonly _SwitchListTileType _switchListTileType;
public override Widget build(BuildContext context) {
Widget control = null;
switch (_switchListTileType) {
case _SwitchListTileType.adaptive:
control = Switch.adaptive(
value: value,
onChanged: onChanged,
activeColor: activeColor,
activeThumbImage: activeThumbImage,
inactiveThumbImage: inactiveThumbImage,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor,
inactiveThumbColor: inactiveThumbColor
);
break;
case _SwitchListTileType.material:
control = new Switch(
value: value,
onChanged: onChanged,
activeColor: activeColor,
activeThumbImage: activeThumbImage,
inactiveThumbImage: inactiveThumbImage,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor,
inactiveThumbColor: inactiveThumbColor
);
break;
}
return ListTileTheme.merge(
selectedColor: activeColor ?? Theme.of(context).accentColor,
child: new ListTile(
leading: secondary,
title: title,
subtitle: subtitle,
trailing: control,
isThreeLine: isThreeLine,
dense: dense,
contentPadding: contentPadding,
enabled: onChanged != null,
onTap: onChanged != null ? () => { onChanged(!value); } : (GestureTapCallback) null,
selected: selected
)
);
}
}
}
正在加载...
取消
保存