Skip to content

Commit

Permalink
Make use of MouseRegion to enforce click cursor (#322)
Browse files Browse the repository at this point in the history
Now requires Flutter 2.2
  • Loading branch information
daohoangson authored May 21, 2021
1 parent c93a63b commit a5a9385
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
5 changes: 4 additions & 1 deletion packages/core/lib/src/core_widget_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ class WidgetFactory {
/// Builds [GestureDetector].
Widget? buildGestureDetector(
BuildMetadata meta, Widget child, GestureTapCallback onTap) =>
GestureDetector(onTap: onTap, child: child);
MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(onTap: onTap, child: child),
);

/// Builds horizontal scroll view.
Widget? buildHorizontalScrollView(BuildMetadata meta, Widget child) =>
Expand Down
2 changes: 2 additions & 0 deletions packages/core/lib/src/internal/flattener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class Flattener {
if (scopedRecognizer != null) _recognizers.add(scopedRecognizer);

_spans!.add((context, whitespace) => TextSpan(
mouseCursor:
scopedRecognizer != null ? SystemMouseCursors.click : null,
recognizer: scopedRecognizer,
style: scopedTsb.build(context).styleWithHeight,
text: scopedStrings.toText(whitespace: whitespace),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Flutter package for widget tree building from html that focuses on
homepage: https://github.com/daohoangson/flutter_widget_from_html/tree/master/packages/core

environment:
flutter: ">=2.0.0"
flutter: ">=2.2.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
Expand Down
9 changes: 6 additions & 3 deletions packages/core/test/_.dart
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,12 @@ class Explainer {
// G-M
attr.add(widget is GestureDetector
? _widgetChild(widget.child)
: widget is MultiChildRenderObjectWidget
? (widget is! RichText ? _widgetChildren(widget.children) : '')
: '');
: widget is MouseRegion
? _widgetChild(widget.child)
: widget is MultiChildRenderObjectWidget
? (widget is! RichText ? _widgetChildren(widget.children) : '')
: '');

// N-T
attr.add(widget is ProxyWidget
? _widgetChild(widget.child)
Expand Down
41 changes: 22 additions & 19 deletions packages/core/test/tag_a_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ void main() {
final explained = await explain(tester, html);
expect(
explained,
equals('[GestureDetector:child='
equals('[MouseRegion:child=[GestureDetector:child='
'[CssBlock:child=[RichText:(#FF123456+u:Foo)]]'
']'),
']]'),
);
});

Expand All @@ -81,8 +81,8 @@ void main() {
explained,
equals(
'[Column:children='
'[GestureDetector:child=[CssBlock:child=[RichText:(#FF123456+u:Foo)]]],'
'[GestureDetector:child=[CssBlock:child=[RichText:(#FF123456+u:Bar)]]]'
'[MouseRegion:child=[GestureDetector:child=[CssBlock:child=[RichText:(#FF123456+u:Foo)]]]],'
'[MouseRegion:child=[GestureDetector:child=[CssBlock:child=[RichText:(#FF123456+u:Bar)]]]]'
']',
),
);
Expand All @@ -99,9 +99,9 @@ void main() {
final explained = await explain(tester, html);
expect(
explained,
equals('[CssBlock:child=[GestureDetector:child='
equals('[CssBlock:child=[MouseRegion:child=[GestureDetector:child='
'[CssBlock:child=[RichText:(#FF123456+u:Foo)]]'
']]'),
']]]'),
);
});

Expand All @@ -112,10 +112,10 @@ void main() {
expect(
explained,
equals(
'[CssBlock:child=[GestureDetector:child=[Column:children='
'[CssBlock:child=[MouseRegion:child=[GestureDetector:child=[Column:children='
'[CssBlock:child=[RichText:(#FF123456+u:Foo)]],'
'[CssBlock:child=[RichText:(#FF123456+u:Bar)]]'
']]]',
']]]]',
),
);
});
Expand All @@ -138,7 +138,10 @@ void main() {
expect(
explained,
equals('[SizedBox:0.0x5.0],'
'[GestureDetector:child=[Padding:(0,5,0,5),child=[CssBlock:child=[RichText:(#FF123456+u:Foo)]]]],'
'[MouseRegion:child=[GestureDetector:child='
'[Padding:(0,5,0,5),child='
'[CssBlock:child=[RichText:(#FF123456+u:Foo)]]'
']]],'
'[SizedBox:0.0x5.0]'));
});

Expand All @@ -152,9 +155,9 @@ void main() {
final explained = await explainImg(tester, html);
expect(
explained,
equals('[GestureDetector:child='
equals('[MouseRegion:child=[GestureDetector:child='
'[CssSizing:$sizingConstraints,child=[Image:image=NetworkImage("$kImgSrc", scale: 1.0)]]'
']'));
']]'));
});

testWidgets('renders text + IMG tag both inside', (tester) async {
Expand All @@ -164,9 +167,9 @@ void main() {
explained,
equals('[RichText:(:'
'(#FF123456+u+onTap:Foo )'
'[GestureDetector:child='
'[MouseRegion:child=[GestureDetector:child='
'[CssSizing:$sizingConstraints,child=[Image:image=NetworkImage("$kImgSrc", scale: 1.0)]]'
'])]'));
']])]'));
});

testWidgets('renders text outside + IMG tag inside', (tester) async {
Expand All @@ -175,9 +178,9 @@ void main() {
expect(
explained,
equals('[RichText:(:Foo '
'[GestureDetector:child='
'[MouseRegion:child=[GestureDetector:child='
'[CssSizing:$sizingConstraints,child=[Image:image=NetworkImage("$kImgSrc", scale: 1.0)]]'
'])]'));
']])]'));
});

testWidgets('renders IMG tag + text both inside', (tester) async {
Expand All @@ -186,9 +189,9 @@ void main() {
expect(
explained,
equals('[RichText:(:'
'[GestureDetector:child='
'[MouseRegion:child=[GestureDetector:child='
'[CssSizing:$sizingConstraints,child=[Image:image=NetworkImage("$kImgSrc", scale: 1.0)]]'
']'
']]'
'(#FF123456+u+onTap: foo)'
')]'));
});
Expand All @@ -199,9 +202,9 @@ void main() {
expect(
explained,
equals('[RichText:(:'
'[GestureDetector:child='
'[MouseRegion:child=[GestureDetector:child='
'[CssSizing:$sizingConstraints,child=[Image:image=NetworkImage("$kImgSrc", scale: 1.0)]]'
']'
']]'
'(: foo))]'));
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/enhanced/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Flutter package for widget tree building from html that supports hy
homepage: https://github.com/daohoangson/flutter_widget_from_html

environment:
flutter: ">=2.0.0"
flutter: ">=2.2.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
Expand Down

1 comment on commit a5a9385

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.