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
c6870ee9
Unverified
Commit
c6870ee9
authored
Aug 29, 2022
by
Chinmoy
Committed by
GitHub
Aug 29, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added onSelectionChange callback to SelectionRegion and SelectionArea (#108985)
parent
43d38bd4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
0 deletions
+101
-0
selection_area.dart
packages/flutter/lib/src/material/selection_area.dart
+6
-0
selectable_region.dart
packages/flutter/lib/src/widgets/selectable_region.dart
+19
-0
selection_area_test.dart
packages/flutter/test/material/selection_area_test.dart
+39
-0
selectable_region_test.dart
packages/flutter/test/widgets/selectable_region_test.dart
+37
-0
No files found.
packages/flutter/lib/src/material/selection_area.dart
View file @
c6870ee9
...
...
@@ -3,6 +3,7 @@
// found in the LICENSE file.
import
'package:flutter/cupertino.dart'
;
import
'package:flutter/rendering.dart'
;
import
'debug.dart'
;
import
'desktop_text_selection.dart'
;
...
...
@@ -41,6 +42,7 @@ class SelectionArea extends StatefulWidget {
this
.
focusNode
,
this
.
selectionControls
,
this
.
magnifierConfiguration
,
this
.
onSelectionChanged
,
required
this
.
child
,
});
...
...
@@ -63,6 +65,9 @@ class SelectionArea extends StatefulWidget {
/// If it is null, the platform specific selection control is used.
final
TextSelectionControls
?
selectionControls
;
/// Called when the selected content changes.
final
ValueChanged
<
SelectedContent
?>?
onSelectionChanged
;
/// The child widget this selection area applies to.
///
/// {@macro flutter.widgets.ProxyWidget.child}
...
...
@@ -112,6 +117,7 @@ class _SelectionAreaState extends State<SelectionArea> {
focusNode:
_effectiveFocusNode
,
selectionControls:
controls
,
magnifierConfiguration:
widget
.
magnifierConfiguration
??
TextMagnifier
.
adaptiveMagnifierConfiguration
,
onSelectionChanged:
widget
.
onSelectionChanged
,
child:
widget
.
child
,
);
}
...
...
packages/flutter/lib/src/widgets/selectable_region.dart
View file @
c6870ee9
...
...
@@ -204,6 +204,7 @@ class SelectableRegion extends StatefulWidget {
required
this
.
selectionControls
,
required
this
.
child
,
this
.
magnifierConfiguration
=
TextMagnifierConfiguration
.
disabled
,
this
.
onSelectionChanged
,
});
/// {@macro flutter.widgets.magnifier.TextMagnifierConfiguration.intro}
...
...
@@ -230,6 +231,9 @@ class SelectableRegion extends StatefulWidget {
/// [TextSelectionControls] implementation with no controls.
final
TextSelectionControls
selectionControls
;
/// Called when the selected content changes.
final
ValueChanged
<
SelectedContent
?>?
onSelectionChanged
;
@override
State
<
StatefulWidget
>
createState
()
=>
_SelectableRegionState
();
}
...
...
@@ -252,6 +256,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
||
_selectionDelegate
.
value
.
endSelectionPoint
!=
null
;
Orientation
?
_lastOrientation
;
SelectedContent
?
_lastSelectedContent
;
@override
void
initState
()
{
...
...
@@ -389,8 +394,16 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
_selectEndTo
(
offset:
details
.
globalPosition
,
continuous:
true
);
}
void
_updateSelectedContentIfNeeded
()
{
if
(
_lastSelectedContent
?.
plainText
!=
_selectable
?.
getSelectedContent
()?.
plainText
)
{
_lastSelectedContent
=
_selectable
?.
getSelectedContent
();
widget
.
onSelectionChanged
?.
call
(
_lastSelectedContent
);
}
}
void
_handleMouseDragEnd
(
DragEndDetails
details
)
{
_finalizeSelection
();
_updateSelectedContentIfNeeded
();
}
void
_handleTouchLongPressStart
(
LongPressStartDetails
details
)
{
...
...
@@ -398,6 +411,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
_selectWordAt
(
offset:
details
.
globalPosition
);
_showToolbar
();
_showHandles
();
_updateSelectedContentIfNeeded
();
}
void
_handleTouchLongPressMoveUpdate
(
LongPressMoveUpdateDetails
details
)
{
...
...
@@ -406,6 +420,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
void
_handleTouchLongPressEnd
(
LongPressEndDetails
details
)
{
_finalizeSelection
();
_updateSelectedContentIfNeeded
();
}
void
_handleRightClickDown
(
TapDownDetails
details
)
{
...
...
@@ -413,6 +428,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
_selectWordAt
(
offset:
details
.
globalPosition
);
_showHandles
();
_showToolbar
(
location:
details
.
globalPosition
);
_updateSelectedContentIfNeeded
();
}
// Selection update helper methods.
...
...
@@ -451,6 +467,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
void
_onAnyDragEnd
(
DragEndDetails
details
)
{
_selectionOverlay
!.
hideMagnifier
(
shouldShowToolbar:
true
);
_stopSelectionEndEdgeUpdate
();
_updateSelectedContentIfNeeded
();
}
void
_stopSelectionEndEdgeUpdate
()
{
...
...
@@ -802,6 +819,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
void
_clearSelection
()
{
_finalizeSelection
();
_selectable
?.
dispatchSelectionEvent
(
const
ClearSelectionEvent
());
_updateSelectedContentIfNeeded
();
}
Future
<
void
>
_copy
()
async
{
...
...
@@ -836,6 +854,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
_showToolbar
();
_showHandles
();
}
_updateSelectedContentIfNeeded
();
}
@override
...
...
packages/flutter/test/material/selection_area_test.dart
View file @
c6870ee9
...
...
@@ -4,9 +4,17 @@
import
'package:flutter/cupertino.dart'
;
import
'package:flutter/foundation.dart'
;
import
'package:flutter/gestures.dart'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/rendering.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
Offset
textOffsetToPosition
(
RenderParagraph
paragraph
,
int
offset
)
{
const
Rect
caret
=
Rect
.
fromLTWH
(
0.0
,
0.0
,
2.0
,
20.0
);
final
Offset
localOffset
=
paragraph
.
getOffsetForCaret
(
TextPosition
(
offset:
offset
),
caret
);
return
paragraph
.
localToGlobal
(
localOffset
);
}
void
main
(
)
{
testWidgets
(
'SelectionArea uses correct selection controls'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
const
MaterialApp
(
...
...
@@ -33,4 +41,35 @@ void main() {
break
;
}
},
variant:
TargetPlatformVariant
.
all
());
testWidgets
(
'onSelectionChange is called when the selection changes'
,
(
WidgetTester
tester
)
async
{
SelectedContent
?
content
;
await
tester
.
pumpWidget
(
MaterialApp
(
home:
SelectionArea
(
child:
const
Text
(
'How are you'
),
onSelectionChanged:
(
SelectedContent
?
selectedContent
)
=>
content
=
selectedContent
,
),
));
final
RenderParagraph
paragraph
=
tester
.
renderObject
<
RenderParagraph
>(
find
.
descendant
(
of:
find
.
text
(
'How are you'
),
matching:
find
.
byType
(
RichText
)));
final
TestGesture
gesture
=
await
tester
.
startGesture
(
textOffsetToPosition
(
paragraph
,
4
),
kind:
PointerDeviceKind
.
mouse
);
expect
(
content
,
isNull
);
addTearDown
(
gesture
.
removePointer
);
await
tester
.
pump
();
await
gesture
.
moveTo
(
textOffsetToPosition
(
paragraph
,
7
));
await
gesture
.
up
();
await
tester
.
pump
();
expect
(
content
,
isNotNull
);
expect
(
content
!.
plainText
,
'are'
);
// Backwards selection.
await
gesture
.
down
(
textOffsetToPosition
(
paragraph
,
3
));
expect
(
content
,
isNull
);
await
gesture
.
moveTo
(
textOffsetToPosition
(
paragraph
,
0
));
await
gesture
.
up
();
await
tester
.
pump
();
expect
(
content
,
isNotNull
);
expect
(
content
!.
plainText
,
'How'
);
});
}
packages/flutter/test/widgets/selectable_region_test.dart
View file @
c6870ee9
...
...
@@ -1208,6 +1208,43 @@ void main() {
skip:
kIsWeb
,
// [intended] Web uses its native context menu.
variant:
const
TargetPlatformVariant
(<
TargetPlatform
>{
TargetPlatform
.
iOS
,
TargetPlatform
.
android
}),
);
testWidgets
(
'onSelectionChange is called when the selection changes'
,
(
WidgetTester
tester
)
async
{
SelectedContent
?
content
;
await
tester
.
pumpWidget
(
MaterialApp
(
home:
SelectableRegion
(
onSelectionChanged:
(
SelectedContent
?
selectedContent
)
=>
content
=
selectedContent
,
focusNode:
FocusNode
(),
selectionControls:
materialTextSelectionControls
,
child:
const
Center
(
child:
Text
(
'How are you'
),
),
),
),
);
final
RenderParagraph
paragraph
=
tester
.
renderObject
<
RenderParagraph
>(
find
.
descendant
(
of:
find
.
text
(
'How are you'
),
matching:
find
.
byType
(
RichText
)));
final
TestGesture
gesture
=
await
tester
.
startGesture
(
textOffsetToPosition
(
paragraph
,
4
),
kind:
PointerDeviceKind
.
mouse
);
expect
(
content
,
isNull
);
addTearDown
(
gesture
.
removePointer
);
await
tester
.
pump
();
await
gesture
.
moveTo
(
textOffsetToPosition
(
paragraph
,
7
));
await
gesture
.
up
();
await
tester
.
pump
();
expect
(
content
,
isNotNull
);
expect
(
content
!.
plainText
,
'are'
);
// Backwards selection.
await
gesture
.
down
(
textOffsetToPosition
(
paragraph
,
3
));
expect
(
content
,
isNull
);
await
gesture
.
moveTo
(
textOffsetToPosition
(
paragraph
,
0
));
await
gesture
.
up
();
await
tester
.
pump
();
expect
(
content
,
isNotNull
);
expect
(
content
!.
plainText
,
'How'
);
});
}
class
SelectionSpy
extends
LeafRenderObjectWidget
{
...
...
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