fix bug on cupertino/segmented_control and fade_in_image

This commit is contained in:
Xingwei Zhu 2022-08-25 17:55:01 +08:00
Родитель a0f70d850b
Коммит 7ce3f39d32
4 изменённых файлов: 657 добавлений и 571 удалений

Просмотреть файл

@ -1,17 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using Unity.UIWidgets.foundation; using Unity.UIWidgets.foundation;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using Unity.UIWidgets.scheduler;
using System;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.painting;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using Rect = Unity.UIWidgets.ui.Rect;
using Transform = Unity.UIWidgets.widgets.Transform;
namespace Unity.UIWidgets.animation { namespace Unity.UIWidgets.animation {
public class TweenSequence<T> : Animatable<T> { public class TweenSequence<T> : Animatable<T> {

Просмотреть файл

@ -11,16 +11,16 @@ using Unity.UIWidgets.rendering;
using UnityEngine; using UnityEngine;
using Color = Unity.UIWidgets.ui.Color; using Color = Unity.UIWidgets.ui.Color;
using Rect = Unity.UIWidgets.ui.Rect; using Rect = Unity.UIWidgets.ui.Rect;
using TextStyle = Unity.UIWidgets.painting.TextStyle;
namespace Unity.UIWidgets.cupertino { namespace Unity.UIWidgets.cupertino {
class CupertinoSegmentedControlsUtils { static class CupertinoSegmentedControlsUtils {
public static readonly EdgeInsetsGeometry _kHorizontalItemPadding = EdgeInsets.symmetric(horizontal: 16.0f); public static readonly EdgeInsetsGeometry _kHorizontalItemPadding = EdgeInsets.symmetric(horizontal: 16.0f);
public const float _kMinSegmentedControlHeight = 28.0f; public const float _kMinSegmentedControlHeight = 28.0f;
public static readonly TimeSpan _kFadeDuration = TimeSpan.FromMilliseconds(165); public static readonly TimeSpan _kFadeDuration = TimeSpan.FromMilliseconds(165);
} }
public class CupertinoSegmentedControl<T> : StatefulWidget { public class CupertinoSegmentedControl<T> : StatefulWidget
{
public CupertinoSegmentedControl( public CupertinoSegmentedControl(
Key key = null, Key key = null,
Dictionary<T, Widget> children = null, Dictionary<T, Widget> children = null,
@ -31,8 +31,8 @@ namespace Unity.UIWidgets.cupertino {
Color borderColor = null, Color borderColor = null,
Color pressedColor = null, Color pressedColor = null,
EdgeInsetsGeometry padding = null EdgeInsetsGeometry padding = null
) :base(key: key) ) : base(key: key) {
{ D.assert(children != null); D.assert(children != null);
D.assert(children.Count >= 2); D.assert(children.Count >= 2);
D.assert(onValueChanged != null); D.assert(onValueChanged != null);
D.assert( D.assert(
@ -47,8 +47,8 @@ namespace Unity.UIWidgets.cupertino {
this.borderColor = borderColor; this.borderColor = borderColor;
this.pressedColor = pressedColor; this.pressedColor = pressedColor;
this.padding = padding; this.padding = padding;
} }
public readonly Dictionary<T, Widget> children; public readonly Dictionary<T, Widget> children;
public readonly T groupValue; public readonly T groupValue;
public readonly ValueChanged<T> onValueChanged; public readonly ValueChanged<T> onValueChanged;
@ -62,11 +62,32 @@ namespace Unity.UIWidgets.cupertino {
return new _SegmentedControlState<T>(); return new _SegmentedControlState<T>();
} }
} }
class _SegmentedControlState<T> : TickerProviderStateMixin<CupertinoSegmentedControl<T>> {
T _pressedKey;
public readonly List<AnimationController> _selectionControllers = new List<AnimationController>(); class _SegmentedControlState<T> : TickerProviderStateMixin<CupertinoSegmentedControl<T>> {
public readonly List<ColorTween> _childTweens = new List<ColorTween>();
T __pressedKey = default;
bool __isNull = true;
void setPressedKey(T newkey) {
__pressedKey = newkey;
__isNull = false;
}
void setPressedKeyNull() {
__pressedKey = default;
__isNull = true;
}
bool isPressedKeyNull() {
return __isNull;
}
T getPressedKey() {
return __pressedKey;
}
readonly List<AnimationController> _selectionControllers = new List<AnimationController>();
readonly List<ColorTween> _childTweens = new List<ColorTween>();
ColorTween _forwardBackgroundColorTween; ColorTween _forwardBackgroundColorTween;
ColorTween _reverseBackgroundColorTween; ColorTween _reverseBackgroundColorTween;
@ -84,7 +105,6 @@ namespace Unity.UIWidgets.cupertino {
); );
controller.addListener(() => { controller.addListener(() => {
setState(() => { setState(() => {
// State of background/text colors has changed
}); });
}); });
return controller; return controller;
@ -92,23 +112,26 @@ namespace Unity.UIWidgets.cupertino {
bool _updateColors() { bool _updateColors() {
D.assert(mounted, () => "This should only be called after didUpdateDependencies"); D.assert(mounted, () => "This should only be called after didUpdateDependencies");
bool changed = false; var changed = false;
Color selectedColor = widget.selectedColor ?? CupertinoTheme.of(context).primaryColor; var selectedColor = widget.selectedColor ?? CupertinoTheme.of(context).primaryColor;
if (_selectedColor != selectedColor) { if (_selectedColor != selectedColor) {
changed = true; changed = true;
_selectedColor = selectedColor; _selectedColor = selectedColor;
} }
Color unselectedColor = widget.unselectedColor ?? CupertinoTheme.of(context).primaryContrastingColor;
var unselectedColor = widget.unselectedColor ?? CupertinoTheme.of(context).primaryContrastingColor;
if (_unselectedColor != unselectedColor) { if (_unselectedColor != unselectedColor) {
changed = true; changed = true;
_unselectedColor = unselectedColor; _unselectedColor = unselectedColor;
} }
Color borderColor = widget.borderColor ?? CupertinoTheme.of(context).primaryColor;
var borderColor = widget.borderColor ?? CupertinoTheme.of(context).primaryColor;
if (_borderColor != borderColor) { if (_borderColor != borderColor) {
changed = true; changed = true;
_borderColor = borderColor; _borderColor = borderColor;
} }
Color pressedColor = widget.pressedColor ?? CupertinoTheme.of(context).primaryColor.withOpacity(0.2f);
var pressedColor = widget.pressedColor ?? CupertinoTheme.of(context).primaryColor.withOpacity(0.2f);
if (_pressedColor != pressedColor) { if (_pressedColor != pressedColor) {
changed = true; changed = true;
_pressedColor = pressedColor; _pressedColor = pressedColor;
@ -122,28 +145,33 @@ namespace Unity.UIWidgets.cupertino {
begin: _unselectedColor, begin: _unselectedColor,
end: _selectedColor end: _selectedColor
); );
_textColorTween = new ColorTween( _textColorTween = new ColorTween(
begin: _selectedColor, begin: _selectedColor,
end: _unselectedColor end: _unselectedColor
); );
return changed; return changed;
} }
void _updateAnimationControllers() { void _updateAnimationControllers() {
D.assert(mounted, () => "This should only be called after didUpdateDependencies"); D.assert(mounted, () => "This should only be called after didUpdateDependencies");
foreach ( AnimationController controller in _selectionControllers) { foreach (var controller in _selectionControllers) {
controller.dispose(); controller.dispose();
} }
_selectionControllers.Clear(); _selectionControllers.Clear();
_childTweens.Clear(); _childTweens.Clear();
foreach ( T key in widget.children.Keys) { foreach (var key in widget.children.Keys) {
AnimationController animationController = createAnimationController(); var animationController = createAnimationController();
if (widget.groupValue.Equals(key)) { if (widget.groupValue.Equals(key)) {
_childTweens.Add(_reverseBackgroundColorTween); _childTweens.Add(_reverseBackgroundColorTween);
animationController.setValue(1.0f); animationController.setValue(1.0f);
} else { }
else {
_childTweens.Add(_forwardBackgroundColorTween); _childTweens.Add(_forwardBackgroundColorTween);
} }
_selectionControllers.Add(animationController); _selectionControllers.Add(animationController);
} }
} }
@ -157,57 +185,65 @@ namespace Unity.UIWidgets.cupertino {
} }
public override void didUpdateWidget(StatefulWidget oldWidget) { public override void didUpdateWidget(StatefulWidget oldWidget) {
oldWidget = (CupertinoSegmentedControl<T>) oldWidget; var _oldWidget = oldWidget as CupertinoSegmentedControl<T>;
base.didUpdateWidget(oldWidget); base.didUpdateWidget(oldWidget);
if (_updateColors() || ((CupertinoSegmentedControl<T>) oldWidget).children.Count != widget.children.Count) { if (_updateColors() || _oldWidget.children.Count != widget.children.Count) {
_updateAnimationControllers(); _updateAnimationControllers();
} }
if (!((CupertinoSegmentedControl<T>)oldWidget).groupValue.Equals(widget.groupValue)) { if (!_oldWidget.groupValue.Equals(widget.groupValue)) {
int index = 0; var index = 0;
foreach ( T key in widget.children.Keys) { foreach (var key in widget.children.Keys) {
if (widget.groupValue.Equals(key)) { if (widget.groupValue.Equals(key)) {
_childTweens[index] = _forwardBackgroundColorTween; _childTweens[index] = _forwardBackgroundColorTween;
_selectionControllers[index].forward(); _selectionControllers[index].forward();
} else { }
else {
_childTweens[index] = _reverseBackgroundColorTween; _childTweens[index] = _reverseBackgroundColorTween;
_selectionControllers[index].reverse(); _selectionControllers[index].reverse();
} }
index += 1; index += 1;
} }
} }
} }
public override void dispose() { public override void dispose() {
foreach( AnimationController animationController in _selectionControllers) { foreach (var animationController in _selectionControllers) {
animationController.dispose(); animationController.dispose();
} }
base.dispose(); base.dispose();
} }
void _onTapDown(T currentKey) { void _onTapDown(T currentKey) {
if (_pressedKey == null && !currentKey.Equals(widget.groupValue)) { if (isPressedKeyNull() && !currentKey.Equals(widget.groupValue)) {
setState(() => { setState(() => {
_pressedKey = currentKey; setPressedKey(currentKey);
}); });
} }
} }
void _onTapCancel() { void _onTapCancel() {
setState(()=> { setState(setPressedKeyNull);
_pressedKey = default(T); }
});
bool isPressKeyEquals(T currentKey) {
return isPressedKeyNull() && currentKey == null ||
!isPressedKeyNull() && currentKey.Equals(getPressedKey());
} }
void _onTap(T currentKey) { void _onTap(T currentKey) {
if (!currentKey.Equals( _pressedKey)) if (!isPressKeyEquals(currentKey))
return; return;
if (!currentKey.Equals(widget.groupValue)) { if (!currentKey.Equals(widget.groupValue)) {
widget.onValueChanged(currentKey); widget.onValueChanged(currentKey);
} }
_pressedKey = default;
setPressedKeyNull();
} }
Color getTextColor(int index, T currentKey) { Color getTextColor(int index, T currentKey) {
@ -223,7 +259,7 @@ namespace Unity.UIWidgets.cupertino {
return _childTweens[index].evaluate(_selectionControllers[index]); return _childTweens[index].evaluate(_selectionControllers[index]);
if (widget.groupValue.Equals(currentKey)) if (widget.groupValue.Equals(currentKey))
return _selectedColor; return _selectedColor;
if (_pressedKey.Equals(currentKey)) if (isPressKeyEquals(currentKey))
return _pressedColor; return _pressedColor;
return _unselectedColor; return _unselectedColor;
} }
@ -232,29 +268,30 @@ namespace Unity.UIWidgets.cupertino {
List<Widget> _gestureChildren = new List<Widget>(); List<Widget> _gestureChildren = new List<Widget>();
List<Color> _backgroundColors = new List<Color>(); List<Color> _backgroundColors = new List<Color>();
int index = 0; int index = 0;
int selectedIndex = 0; int? selectedIndex = null;
int pressedIndex = 0; int? pressedIndex = null;
foreach ( T currentKey in widget.children.Keys) { foreach (var currentKey in widget.children.Keys) {
selectedIndex = (widget.groupValue.Equals(currentKey)) ? index : selectedIndex; var currentKey2 = currentKey;
pressedIndex = (_pressedKey.Equals(currentKey)) ? index : pressedIndex; selectedIndex = (widget.groupValue.Equals(currentKey2)) ? index : selectedIndex;
TextStyle textStyle = DefaultTextStyle.of(context).style.copyWith( pressedIndex = (isPressKeyEquals(currentKey2)) ? index : pressedIndex;
color: getTextColor(index, currentKey) var textStyle = DefaultTextStyle.of(context).style.copyWith(
color: getTextColor(index, currentKey2)
); );
IconThemeData iconTheme = new IconThemeData( var iconTheme = new IconThemeData(
color: getTextColor(index, currentKey) color: getTextColor(index, currentKey2)
); );
Widget child = new Center( Widget child = new Center(
child: widget.children[currentKey] child: widget.children[currentKey2]
); );
child = new GestureDetector( child = new GestureDetector(
onTapDown: (TapDownDetails _event) => { onTapDown: (TapDownDetails _event) => {
_onTapDown(currentKey); _onTapDown(currentKey2);
}, },
onTapCancel: _onTapCancel, onTapCancel: _onTapCancel,
onTap: () => { onTap: () => {
_onTap(currentKey); _onTap(currentKey2);
}, },
child: new IconTheme( child: new IconTheme(
data: iconTheme, data: iconTheme,
@ -265,7 +302,7 @@ namespace Unity.UIWidgets.cupertino {
) )
); );
_backgroundColors.Add(getBackgroundColor(index, currentKey)); _backgroundColors.Add(getBackgroundColor(index, currentKey2));
_gestureChildren.Add(child); _gestureChildren.Add(child);
index += 1; index += 1;
} }
@ -287,6 +324,7 @@ namespace Unity.UIWidgets.cupertino {
); );
} }
} }
public class _SegmentedControlRenderWidget<T> : MultiChildRenderObjectWidget { public class _SegmentedControlRenderWidget<T> : MultiChildRenderObjectWidget {
public _SegmentedControlRenderWidget( public _SegmentedControlRenderWidget(
Key key = null, Key key = null,
@ -309,6 +347,7 @@ namespace Unity.UIWidgets.cupertino {
public readonly int? pressedIndex; public readonly int? pressedIndex;
public readonly List<Color> backgroundColors; public readonly List<Color> backgroundColors;
public readonly Color borderColor; public readonly Color borderColor;
public override RenderObject createRenderObject(BuildContext context) { public override RenderObject createRenderObject(BuildContext context) {
return new _RenderSegmentedControl<T>( return new _RenderSegmentedControl<T>(
textDirection: Directionality.of(context), textDirection: Directionality.of(context),
@ -318,6 +357,7 @@ namespace Unity.UIWidgets.cupertino {
borderColor: borderColor borderColor: borderColor
); );
} }
public override void updateRenderObject(BuildContext context, RenderObject renderObject) { public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
renderObject = (_RenderSegmentedControl<T>) renderObject; renderObject = (_RenderSegmentedControl<T>) renderObject;
((_RenderSegmentedControl<T>) renderObject).textDirection = Directionality.of(context); ((_RenderSegmentedControl<T>) renderObject).textDirection = Directionality.of(context);
@ -325,7 +365,6 @@ namespace Unity.UIWidgets.cupertino {
((_RenderSegmentedControl<T>) renderObject).pressedIndex = pressedIndex; ((_RenderSegmentedControl<T>) renderObject).pressedIndex = pressedIndex;
((_RenderSegmentedControl<T>) renderObject).backgroundColors = backgroundColors; ((_RenderSegmentedControl<T>) renderObject).backgroundColors = backgroundColors;
((_RenderSegmentedControl<T>) renderObject).borderColor = borderColor; ((_RenderSegmentedControl<T>) renderObject).borderColor = borderColor;
} }
} }
@ -358,71 +397,70 @@ namespace Unity.UIWidgets.cupertino {
if (_selectedIndex == value) { if (_selectedIndex == value) {
return; return;
} }
_selectedIndex = value; _selectedIndex = value;
markNeedsPaint(); markNeedsPaint();
} }
} }
int? _selectedIndex; int? _selectedIndex;
public int? pressedIndex { public int? pressedIndex {
get { get { return _pressedIndex; }
return _pressedIndex;
}
set { set {
if (_pressedIndex == value) { if (_pressedIndex == value) {
return; return;
} }
_pressedIndex = value; _pressedIndex = value;
markNeedsPaint(); markNeedsPaint();
} }
} }
int? _pressedIndex; int? _pressedIndex;
public TextDirection? textDirection { public TextDirection? textDirection {
get { get { return _textDirection; }
return _textDirection;
}
set { set {
if (_textDirection == value) { if (_textDirection == value) {
return; return;
} }
_textDirection = value; _textDirection = value;
markNeedsLayout(); markNeedsLayout();
} }
} }
TextDirection? _textDirection; TextDirection? _textDirection;
public List<Color> backgroundColors { public List<Color> backgroundColors {
get { get { return _backgroundColors; }
return _backgroundColors;
}
set { set {
if (_backgroundColors == value) { if (_backgroundColors == value) {
return; return;
} }
_backgroundColors = value; _backgroundColors = value;
markNeedsPaint(); markNeedsPaint();
} }
} }
List<Color> _backgroundColors; List<Color> _backgroundColors;
public Color borderColor { public Color borderColor {
get { get { return _borderColor; }
return _borderColor;
}
set { set {
if (_borderColor == value) { if (_borderColor == value) {
return; return;
} }
_borderColor = value; _borderColor = value;
markNeedsPaint(); markNeedsPaint();
}
} }
}
Color _borderColor; Color _borderColor;
@ -430,11 +468,13 @@ namespace Unity.UIWidgets.cupertino {
RenderBox child = firstChild; RenderBox child = firstChild;
float minWidth = 0.0f; float minWidth = 0.0f;
while (child != null) { while (child != null) {
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
float childWidth = child.getMinIntrinsicWidth(height); float childWidth = child.getMinIntrinsicWidth(height);
minWidth = Mathf.Max(minWidth, childWidth); minWidth = Mathf.Max(minWidth, childWidth);
child = childParentData.nextSibling; child = childParentData.nextSibling;
} }
return minWidth * childCount; return minWidth * childCount;
} }
@ -442,11 +482,13 @@ namespace Unity.UIWidgets.cupertino {
RenderBox child = firstChild; RenderBox child = firstChild;
float maxWidth = 0.0f; float maxWidth = 0.0f;
while (child != null) { while (child != null) {
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
float childWidth = child.getMaxIntrinsicWidth(height); float childWidth = child.getMaxIntrinsicWidth(height);
maxWidth = Mathf.Max(maxWidth, childWidth); maxWidth = Mathf.Max(maxWidth, childWidth);
child = childParentData.nextSibling; child = childParentData.nextSibling;
} }
return maxWidth * childCount; return maxWidth * childCount;
} }
@ -454,22 +496,27 @@ namespace Unity.UIWidgets.cupertino {
RenderBox child = firstChild; RenderBox child = firstChild;
float minHeight = 0.0f; float minHeight = 0.0f;
while (child != null) { while (child != null) {
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
float childHeight = child.getMinIntrinsicHeight(width); float childHeight = child.getMinIntrinsicHeight(width);
minHeight = Mathf.Max(minHeight, childHeight); minHeight = Mathf.Max(minHeight, childHeight);
child = childParentData.nextSibling; child = childParentData.nextSibling;
} }
return minHeight; return minHeight;
} }
protected internal override float computeMaxIntrinsicHeight(float width) { protected internal override float computeMaxIntrinsicHeight(float width) {
RenderBox child = firstChild; RenderBox child = firstChild;
float maxHeight = 0.0f; float maxHeight = 0.0f;
while (child != null) { while (child != null) {
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
float childHeight = child.getMaxIntrinsicHeight(width); float childHeight = child.getMaxIntrinsicHeight(width);
maxHeight = Mathf.Max(maxHeight, childHeight); maxHeight = Mathf.Max(maxHeight, childHeight);
child = childParentData.nextSibling; child = childParentData.nextSibling;
} }
return maxHeight; return maxHeight;
} }
@ -488,7 +535,8 @@ namespace Unity.UIWidgets.cupertino {
RenderBox child = leftChild; RenderBox child = leftChild;
float start = 0.0f; float start = 0.0f;
while (child != null) { while (child != null) {
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
Offset childOffset = new Offset(start, 0.0f); Offset childOffset = new Offset(start, 0.0f);
childParentData.offset = childOffset; childParentData.offset = childOffset;
Rect childRect = Rect.fromLTWH(start, 0.0f, child.size.width, child.size.height); Rect childRect = Rect.fromLTWH(start, 0.0f, child.size.width, child.size.height);
@ -496,17 +544,21 @@ namespace Unity.UIWidgets.cupertino {
if (child == leftChild) { if (child == leftChild) {
rChildRect = RRect.fromRectAndCorners(childRect, topLeft: Radius.circular(3.0f), rChildRect = RRect.fromRectAndCorners(childRect, topLeft: Radius.circular(3.0f),
bottomLeft: Radius.circular(3.0f)); bottomLeft: Radius.circular(3.0f));
} else if (child == rightChild) { }
else if (child == rightChild) {
rChildRect = RRect.fromRectAndCorners(childRect, topRight: Radius.circular(3.0f), rChildRect = RRect.fromRectAndCorners(childRect, topRight: Radius.circular(3.0f),
bottomRight: Radius.circular(3.0f)); bottomRight: Radius.circular(3.0f));
} else { }
else {
rChildRect = RRect.fromRectAndCorners(childRect, topRight: Radius.zero); rChildRect = RRect.fromRectAndCorners(childRect, topRight: Radius.zero);
} }
childParentData.surroundingRect = rChildRect; childParentData.surroundingRect = rChildRect;
start += child.size.width; start += child.size.width;
child = nextChild(child); child = nextChild(child);
} }
} }
protected override void performLayout() { protected override void performLayout() {
BoxConstraints constraints = this.constraints; BoxConstraints constraints = this.constraints;
float maxHeight = CupertinoSegmentedControlsUtils._kMinSegmentedControlHeight; float maxHeight = CupertinoSegmentedControlsUtils._kMinSegmentedControlHeight;
@ -515,6 +567,7 @@ namespace Unity.UIWidgets.cupertino {
foreach (RenderBox child in getChildrenAsList()) { foreach (RenderBox child in getChildrenAsList()) {
childWidth = Mathf.Max(childWidth, child.getMaxIntrinsicWidth(float.PositiveInfinity)); childWidth = Mathf.Max(childWidth, child.getMaxIntrinsicWidth(float.PositiveInfinity));
} }
childWidth = Mathf.Min(childWidth, constraints.maxWidth / childCount); childWidth = Mathf.Min(childWidth, constraints.maxWidth / childCount);
RenderBox child1 = firstChild; RenderBox child1 = firstChild;
@ -556,6 +609,7 @@ namespace Unity.UIWidgets.cupertino {
size = constraints.constrain(new Size(childWidth * childCount, maxHeight)); size = constraints.constrain(new Size(childWidth * childCount, maxHeight));
} }
public override void paint(PaintingContext context, Offset offset) { public override void paint(PaintingContext context, Offset offset) {
RenderBox child = firstChild; RenderBox child = firstChild;
int index = 0; int index = 0;
@ -569,20 +623,23 @@ namespace Unity.UIWidgets.cupertino {
void _paintChild(PaintingContext context, Offset offset, RenderBox child, int childIndex) { void _paintChild(PaintingContext context, Offset offset, RenderBox child, int childIndex) {
D.assert(child != null); D.assert(child != null);
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
context.canvas.drawRRect( context.canvas.drawRRect(
childParentData.surroundingRect.shift(offset), childParentData.surroundingRect.shift(offset),
new Paint() { new Paint() {
color = backgroundColors[childIndex], color = backgroundColors[childIndex],
style = PaintingStyle.fill} style = PaintingStyle.fill
}
); );
context.canvas.drawRRect( context.canvas.drawRRect(
childParentData.surroundingRect.shift(offset), childParentData.surroundingRect.shift(offset),
new Paint() { new Paint() {
color = borderColor, color = borderColor,
strokeWidth = 1.0f, strokeWidth = 1.0f,
style = PaintingStyle.stroke} style = PaintingStyle.stroke
}
); );
context.paintChild(child, childParentData.offset + offset); context.paintChild(child, childParentData.offset + offset);
@ -592,7 +649,8 @@ namespace Unity.UIWidgets.cupertino {
D.assert(position != null); D.assert(position != null);
RenderBox child = lastChild; RenderBox child = lastChild;
while (child != null) { while (child != null) {
_SegmentedControlContainerBoxParentData childParentData = child.parentData as _SegmentedControlContainerBoxParentData; _SegmentedControlContainerBoxParentData childParentData =
child.parentData as _SegmentedControlContainerBoxParentData;
if (childParentData.surroundingRect.contains(position)) { if (childParentData.surroundingRect.contains(position)) {
Offset center = (Offset.zero & child.size).center; Offset center = (Offset.zero & child.size).center;
return result.addWithRawTransform( return result.addWithRawTransform(
@ -604,15 +662,11 @@ namespace Unity.UIWidgets.cupertino {
} }
); );
} }
child = childParentData.previousSibling; child = childParentData.previousSibling;
} }
return false; return false;
} }
} }
} }

Просмотреть файл

@ -1,15 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.UIWidgets.animation; using Unity.UIWidgets.animation;
using Unity.UIWidgets.foundation; using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting; using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering; using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui; using Unity.UIWidgets.ui;
using UnityEngine; using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using Debug = System.Diagnostics.Debug;
using Object = System.Object;
namespace Unity.UIWidgets.widgets { namespace Unity.UIWidgets.widgets {
public class FadeInImage : StatelessWidget { public class FadeInImage : StatelessWidget {
@ -30,7 +26,6 @@ namespace Unity.UIWidgets.widgets {
Key key = null, Key key = null,
bool matchTextDirection = false bool matchTextDirection = false
) : base(key) { ) : base(key) {
D.assert(image != null); D.assert(image != null);
D.assert(fadeOutDuration != null); D.assert(fadeOutDuration != null);
D.assert(fadeOutCurve != null); D.assert(fadeOutCurve != null);
@ -128,7 +123,6 @@ namespace Unity.UIWidgets.widgets {
int? imageCacheWidth = null, int? imageCacheWidth = null,
int? imageCacheHeight = null int? imageCacheHeight = null
) { ) {
fadeOutDuration = fadeOutDuration ?? new TimeSpan(0, 0, 0, 0, 300); fadeOutDuration = fadeOutDuration ?? new TimeSpan(0, 0, 0, 0, 300);
fadeOutCurve = fadeOutCurve ?? Curves.easeOut; fadeOutCurve = fadeOutCurve ?? Curves.easeOut;
fadeInDuration = fadeInDuration ?? new TimeSpan(0, 0, 0, 0, 700); fadeInDuration = fadeInDuration ?? new TimeSpan(0, 0, 0, 0, 700);
@ -143,7 +137,61 @@ namespace Unity.UIWidgets.widgets {
return new FadeInImage( return new FadeInImage(
placeholder: _placeholder, placeholder: _placeholder,
placeholderErrorBuilder: placeholderErrorBuilder, placeholderErrorBuilder: placeholderErrorBuilder,
image: ResizeImage.resizeIfNeeded(imageCacheWidth, imageCacheHeight, new NetworkImage(image, scale: imageScale)), image: ResizeImage.resizeIfNeeded(imageCacheWidth, imageCacheHeight,
new NetworkImage(image, scale: imageScale)),
imageErrorBuilder: imageErrorBuilder,
fadeOutDuration: fadeOutDuration,
fadeOutCurve: fadeOutCurve,
fadeInDuration: fadeInDuration,
fadeInCurve: fadeInCurve,
width: width, height: height,
fit: fit,
alignment: alignment,
repeat: repeat,
matchTextDirection: matchTextDirection,
key: key
);
}
public static FadeInImage fileNetwork(
string placeholder,
string image,
ImageErrorWidgetBuilder placeholderErrorBuilder = null,
ImageErrorWidgetBuilder imageErrorBuilder = null,
float? placeholderScale = null,
float imageScale = 1.0f,
TimeSpan? fadeOutDuration = null,
Curve fadeOutCurve = null,
TimeSpan? fadeInDuration = null,
Curve fadeInCurve = null,
float? width = null,
float? height = null,
BoxFit? fit = null,
AlignmentGeometry alignment = null,
ImageRepeat repeat = ImageRepeat.noRepeat,
bool matchTextDirection = false,
Key key = null,
int? placeholderCacheWidth = null,
int? placeholderCacheHeight = null,
int? imageCacheWidth = null,
int? imageCacheHeight = null
) {
fadeOutDuration = fadeOutDuration ?? new TimeSpan(0, 0, 0, 0, 300);
fadeOutCurve = fadeOutCurve ?? Curves.easeOut;
fadeInDuration = fadeInDuration ?? new TimeSpan(0, 0, 0, 0, 700);
fadeInCurve = Curves.easeIn;
alignment = alignment ?? Alignment.center;
var holder = placeholderScale ?? 1.0f;
var _placeholder = placeholderScale != null
? ResizeImage.resizeIfNeeded(placeholderCacheWidth, placeholderCacheHeight,
new FileImage(placeholder, scale: holder))
: ResizeImage.resizeIfNeeded(placeholderCacheWidth, placeholderCacheHeight,
new FileImage(placeholder));
return new FadeInImage(
placeholder: _placeholder,
placeholderErrorBuilder: placeholderErrorBuilder,
image: ResizeImage.resizeIfNeeded(imageCacheWidth, imageCacheHeight,
new NetworkImage(image, scale: imageScale)),
imageErrorBuilder: imageErrorBuilder, imageErrorBuilder: imageErrorBuilder,
fadeOutDuration: fadeOutDuration, fadeOutDuration: fadeOutDuration,
fadeOutCurve: fadeOutCurve, fadeOutCurve: fadeOutCurve,
@ -190,7 +238,6 @@ namespace Unity.UIWidgets.widgets {
repeat: repeat, repeat: repeat,
matchTextDirection: matchTextDirection, matchTextDirection: matchTextDirection,
gaplessPlayback: true gaplessPlayback: true
); );
} }
@ -227,7 +274,6 @@ namespace Unity.UIWidgets.widgets {
Curve fadeInCurve, Curve fadeInCurve,
Key key = null Key key = null
) : base(key: key, duration: fadeInDuration + fadeOutDuration) { ) : base(key: key, duration: fadeInDuration + fadeOutDuration) {
this.target = target; this.target = target;
this.placeholder = placeholder; this.placeholder = placeholder;
this.isTargetLoaded = isTargetLoaded; this.isTargetLoaded = isTargetLoaded;
@ -260,30 +306,30 @@ namespace Unity.UIWidgets.widgets {
state: this, state: this,
tween: _targetOpacity, tween: _targetOpacity,
targetValue: widget.isTargetLoaded ? 1.0f : 0.0f, targetValue: widget.isTargetLoaded ? 1.0f : 0.0f,
constructor: (float value) => new FloatTween(begin: value, 0)); constructor: (float value) => new FloatTween(begin: 1 - value, 0)); //TODO: in flutter it is "new FloatTween(begin: value, 0)", which doesn't work out in UIWidgets
_placeholderOpacity = (FloatTween) visitor.visit( _placeholderOpacity = (FloatTween) visitor.visit(
state: this, state: this,
tween: _placeholderOpacity, tween: _placeholderOpacity,
targetValue: widget.isTargetLoaded ? 0.0f : 1.0f, targetValue: widget.isTargetLoaded ? 0.0f : 1.0f,
constructor: (float value) => new FloatTween(begin: value, 0)); constructor: (float value) => new FloatTween(begin: 1 - value, 0)); //TODO: in flutter it is "new FloatTween(begin: value, 0)", which doesn't work out in UIWidgets
} }
protected override void didUpdateTweens() { protected override void didUpdateTweens() {
List<TweenSequenceItem<float>> list = new List<TweenSequenceItem<float>>(); List<TweenSequenceItem<float>> list = new List<TweenSequenceItem<float>>();
Debug.Assert(widget.fadeOutDuration?.Milliseconds != null, Debug.Assert(widget.fadeOutDuration?.TotalMilliseconds != null,
"widget.fadeOutDuration?.Milliseconds != null"); "widget.fadeOutDuration?.Milliseconds != null");
list.Add(new TweenSequenceItem<float>( list.Add(new TweenSequenceItem<float>(
tween: _placeholderOpacity.chain(new CurveTween(curve: widget.fadeOutCurve)), tween: _placeholderOpacity.chain(new CurveTween(curve: widget.fadeOutCurve)),
weight: (float) widget.fadeOutDuration?.Milliseconds weight: (float) widget.fadeOutDuration?.TotalMilliseconds
)); ));
Debug.Assert(widget.fadeInDuration?.Milliseconds != null, Debug.Assert(widget.fadeInDuration?.TotalMilliseconds != null,
"widget.fadeInDuration?.Milliseconds != null"); "widget.fadeInDuration?.Milliseconds != null");
list.Add(new TweenSequenceItem<float>( list.Add(new TweenSequenceItem<float>(
tween: new ConstantTween<float>(0), tween: new ConstantTween<float>(0),
weight: (float) widget.fadeInDuration?.Milliseconds weight: (float) widget.fadeInDuration?.TotalMilliseconds
)); ));
@ -297,11 +343,11 @@ namespace Unity.UIWidgets.widgets {
List<TweenSequenceItem<float>> list2 = new List<TweenSequenceItem<float>>(); List<TweenSequenceItem<float>> list2 = new List<TweenSequenceItem<float>>();
list2.Add(new TweenSequenceItem<float>( list2.Add(new TweenSequenceItem<float>(
tween: new ConstantTween<float>(0), tween: new ConstantTween<float>(0),
weight: (float) widget.fadeOutDuration?.Milliseconds weight: (float) widget.fadeOutDuration?.TotalMilliseconds
)); ));
list2.Add(new TweenSequenceItem<float>( list2.Add(new TweenSequenceItem<float>(
tween: _targetOpacity.chain(new CurveTween(curve: widget.fadeInCurve)), tween: _targetOpacity.chain(new CurveTween(curve: widget.fadeInCurve)),
weight: (float) widget.fadeInDuration?.Milliseconds weight: (float) widget.fadeInDuration?.TotalMilliseconds
)); ));
_targetOpacityAnimation = animation.drive(new TweenSequence<float>(list2)); _targetOpacityAnimation = animation.drive(new TweenSequence<float>(list2));
if (!widget.isTargetLoaded && _isValid(_placeholderOpacity) && _isValid(_targetOpacity)) { if (!widget.isTargetLoaded && _isValid(_placeholderOpacity) && _isValid(_targetOpacity)) {
@ -313,7 +359,6 @@ namespace Unity.UIWidgets.widgets {
return tween?.begin != null && tween?.end != null; return tween?.begin != null && tween?.end != null;
} }
public override Widget build(BuildContext context) { public override Widget build(BuildContext context) {
Widget target = new FadeTransition( Widget target = new FadeTransition(
opacity: _targetOpacityAnimation, opacity: _targetOpacityAnimation,
@ -345,6 +390,5 @@ namespace Unity.UIWidgets.widgets {
new DiagnosticsProperty<Animation<float>>("placeholderOpacity", _placeholderOpacityAnimation)); new DiagnosticsProperty<Animation<float>>("placeholderOpacity", _placeholderOpacityAnimation));
} }
} }
} }
} }

Просмотреть файл

@ -339,7 +339,7 @@ namespace Unity.UIWidgets.widgets {
} }
public class _ImageState : State<Image>, WidgetsBindingObserver { public class _ImageState : State<Image>, WidgetsBindingObserver {
int _frameNumber; int? _frameNumber = null;
ImageInfo _imageInfo; ImageInfo _imageInfo;
ImageStream _imageStream; ImageStream _imageStream;
@ -584,7 +584,7 @@ namespace Unity.UIWidgets.widgets {
description.add(new DiagnosticsProperty<ImageStream>("stream", value: _imageStream)); description.add(new DiagnosticsProperty<ImageStream>("stream", value: _imageStream));
description.add(new DiagnosticsProperty<ImageInfo>("pixels", value: _imageInfo)); description.add(new DiagnosticsProperty<ImageInfo>("pixels", value: _imageInfo));
description.add(new DiagnosticsProperty<ImageChunkEvent>("loadingProgress", value: _loadingProgress)); description.add(new DiagnosticsProperty<ImageChunkEvent>("loadingProgress", value: _loadingProgress));
description.add(new DiagnosticsProperty<int>("frameNumber", value: _frameNumber)); description.add(new DiagnosticsProperty<int?>("frameNumber", value: _frameNumber));
description.add(new DiagnosticsProperty<bool>("wasSynchronouslyLoaded", value: _wasSynchronouslyLoaded)); description.add(new DiagnosticsProperty<bool>("wasSynchronouslyLoaded", value: _wasSynchronouslyLoaded));
} }
} }