Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
6d07b1b4
Unverified
Commit
6d07b1b4
authored
Jul 28, 2020
by
Ming Lyu (CareF)
Committed by
GitHub
Jul 28, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add WidgetController.scrollUntilVisible (#62097)
parent
aa771d50
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
0 deletions
+136
-0
controller.dart
packages/flutter_test/lib/src/controller.dart
+45
-0
controller_test.dart
packages/flutter_test/test/controller_test.dart
+91
-0
No files found.
packages/flutter_test/lib/src/controller.dart
View file @
6d07b1b4
...
...
@@ -699,6 +699,51 @@ abstract class WidgetController {
///
/// Shorthand for `Scrollable.ensureVisible(element(finder))`
Future
<
void
>
ensureVisible
(
Finder
finder
)
=>
Scrollable
.
ensureVisible
(
element
(
finder
));
/// Repeatedly scrolls the `scrollable` by `delta` in the
/// [Scrpllable.axisDirection] until `finder` is visible.
///
/// Between each scroll, wait for `duration` time for settling.
///
/// Throws a [StateError] if `finder` is not found for maximum `maxScrolls`
/// times.
///
/// This is different from [ensureVisible] in that this allows looking for
/// `finder` that is not built yet, but the caller must specify the scrollable
/// that will build child specified by `finder`.
Future
<
void
>
scrollUntilVisible
(
Finder
finder
,
Finder
scrollable
,
double
delta
,
{
int
maxScrolls
=
50
,
Duration
duration
=
const
Duration
(
milliseconds:
50
),
}
)
{
assert
(
maxScrolls
>
0
);
return
TestAsyncUtils
.
guard
<
void
>(()
async
{
Offset
moveStep
;
switch
(
widget
<
Scrollable
>(
scrollable
).
axisDirection
)
{
case
AxisDirection
.
up
:
moveStep
=
Offset
(
0
,
delta
);
break
;
case
AxisDirection
.
down
:
moveStep
=
Offset
(
0
,
-
delta
);
break
;
case
AxisDirection
.
left
:
moveStep
=
Offset
(
delta
,
0
);
break
;
case
AxisDirection
.
right
:
moveStep
=
Offset
(-
delta
,
0
);
break
;
}
while
(
maxScrolls
>
0
&&
finder
.
evaluate
().
isEmpty
)
{
await
drag
(
scrollable
,
moveStep
);
await
pump
(
duration
);
maxScrolls
-=
1
;
}
await
Scrollable
.
ensureVisible
(
element
(
finder
));
});
}
}
/// Variant of [WidgetController] that can be used in tests running
...
...
packages/flutter_test/test/controller_test.dart
View file @
6d07b1b4
...
...
@@ -439,4 +439,95 @@ void main() {
expect
(
find
.
text
(
'Item 15'
,
skipOffstage:
true
),
findsOneWidget
);
},
);
group
(
'scrollUntilVisible: scrolls to make unbuilt widget visible'
,
()
{
testWidgets
(
'Vertical'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
MaterialApp
(
home:
Scaffold
(
body:
ListView
.
builder
(
itemCount:
50
,
shrinkWrap:
true
,
itemBuilder:
(
BuildContext
context
,
int
i
)
=>
ListTile
(
title:
Text
(
'Item
$i
'
)),
),
),
),
);
// Make sure widget isn't built yet.
expect
(
find
.
text
(
'Item 45'
,
skipOffstage:
false
),
findsNothing
);
await
tester
.
scrollUntilVisible
(
find
.
text
(
'Item 45'
,
skipOffstage:
false
),
find
.
byType
(
Scrollable
),
100
,
);
await
tester
.
pumpAndSettle
();
// Now the widget is on screen.
expect
(
find
.
text
(
'Item 45'
,
skipOffstage:
true
),
findsOneWidget
);
},
);
testWidgets
(
'Horizontal'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
MaterialApp
(
home:
Scaffold
(
body:
ListView
.
builder
(
itemCount:
50
,
shrinkWrap:
true
,
scrollDirection:
Axis
.
horizontal
,
// ListTile does not support horizontal list
itemBuilder:
(
BuildContext
context
,
int
i
)
=>
Container
(
child:
Text
(
'Item
$i
'
)),
),
),
),
);
// Make sure widget isn't built yet.
expect
(
find
.
text
(
'Item 45'
,
skipOffstage:
false
),
findsNothing
);
await
tester
.
scrollUntilVisible
(
find
.
text
(
'Item 45'
,
skipOffstage:
false
),
find
.
byType
(
Scrollable
),
100
,
);
await
tester
.
pumpAndSettle
();
// Now the widget is on screen.
expect
(
find
.
text
(
'Item 45'
,
skipOffstage:
true
),
findsOneWidget
);
},
);
testWidgets
(
'Fail'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
MaterialApp
(
home:
Scaffold
(
body:
ListView
.
builder
(
itemCount:
50
,
shrinkWrap:
true
,
itemBuilder:
(
BuildContext
context
,
int
i
)
=>
ListTile
(
title:
Text
(
'Item
$i
'
)),
),
),
),
);
try
{
await
tester
.
scrollUntilVisible
(
find
.
text
(
'Item 55'
,
skipOffstage:
false
),
find
.
byType
(
Scrollable
),
100
,
);
}
on
StateError
catch
(
e
)
{
expect
(
e
.
message
,
'No element'
);
}
},
);
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment