Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url and url_target to controls with on_click event #1337

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions package/lib/src/controls/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import '../utils/colors.dart';
import '../utils/edge_insets.dart';
import '../utils/gradient.dart';
import '../utils/images.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';
import 'error.dart';

Expand Down Expand Up @@ -45,6 +46,8 @@ class ContainerControl extends StatelessWidget {
children.where((c) => c.name == "content" && c.isVisible);
bool ink = control.attrBool("ink", false)!;
bool onClick = control.attrBool("onclick", false)!;
String url = control.attrString("url", "")!;
String? urlTarget = control.attrString("urlTarget");
bool onLongPress = control.attrBool("onLongPress", false)!;
bool onHover = control.attrBool("onHover", false)!;
bool disabled = control.isDisabled || parentDisabled;
Expand Down Expand Up @@ -126,26 +129,33 @@ class ContainerControl extends StatelessWidget {

Widget? result;

if ((onClick || onLongPress || onHover) && ink && !disabled) {
if ((onClick || url != "" || onLongPress || onHover) &&
ink &&
!disabled) {
var ink = Ink(
decoration: boxDecor,
child: InkWell(
// Dummy callback to enable widget
// see https://github.com/flutter/flutter/issues/50116#issuecomment-582047374
// and https://github.com/flutter/flutter/blob/eed80afe2c641fb14b82a22279d2d78c19661787/packages/flutter/lib/src/material/ink_well.dart#L1125-L1129
onTap: onHover ? () {} : null,
onTapDown: onClick
onTapDown: onClick || url != ""
? (details) {
debugPrint("Container ${control.id} clicked!");
server.sendPageEvent(
eventTarget: control.id,
eventName: "click",
eventData: json.encode(ContainerTapEvent(
localX: details.localPosition.dx,
localY: details.localPosition.dy,
globalX: details.globalPosition.dx,
globalY: details.globalPosition.dy)
.toJson()));
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
if (onClick) {
server.sendPageEvent(
eventTarget: control.id,
eventName: "click",
eventData: json.encode(ContainerTapEvent(
localX: details.localPosition.dx,
localY: details.localPosition.dy,
globalX: details.globalPosition.dx,
globalY: details.globalPosition.dy)
.toJson()));
}
}
: null,
onLongPress: onLongPress
Expand Down Expand Up @@ -230,9 +240,11 @@ class ContainerControl extends StatelessWidget {
: null,
child: child);

if ((onClick || onLongPress || onHover) && !disabled) {
if ((onClick || onLongPress || onHover || url != "") && !disabled) {
result = MouseRegion(
cursor: onClick ? SystemMouseCursors.click : MouseCursor.defer,
cursor: onClick || url != ""
? SystemMouseCursors.click
: MouseCursor.defer,
onEnter: onHover
? (value) {
debugPrint(
Expand All @@ -254,18 +266,23 @@ class ContainerControl extends StatelessWidget {
}
: null,
child: GestureDetector(
onTapDown: onClick
onTapDown: onClick || url != ""
? (details) {
debugPrint("Container ${control.id} clicked!");
server.sendPageEvent(
eventTarget: control.id,
eventName: "click",
eventData: json.encode(ContainerTapEvent(
localX: details.localPosition.dx,
localY: details.localPosition.dy,
globalX: details.globalPosition.dx,
globalY: details.globalPosition.dy)
.toJson()));
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
if (onClick) {
server.sendPageEvent(
eventTarget: control.id,
eventName: "click",
eventData: json.encode(ContainerTapEvent(
localX: details.localPosition.dx,
localY: details.localPosition.dy,
globalX: details.globalPosition.dx,
globalY: details.globalPosition.dy)
.toJson()));
}
}
: null,
onLongPress: onLongPress
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/controls/elevated_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../models/control.dart';
import '../utils/buttons.dart';
import '../utils/colors.dart';
import '../utils/icons.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';
import 'error.dart';

Expand Down Expand Up @@ -58,6 +59,7 @@ class _ElevatedButtonControlState extends State<ElevatedButtonControl> {
final server = FletAppServices.of(context).server;

String text = widget.control.attrString("text", "")!;
String url = widget.control.attrString("url", "")!;
IconData? icon = getMaterialIcon(widget.control.attrString("icon", "")!);
Color? iconColor = HexColor.fromString(
Theme.of(context), widget.control.attrString("iconColor", "")!);
Expand All @@ -70,6 +72,10 @@ class _ElevatedButtonControlState extends State<ElevatedButtonControl> {
Function()? onPressed = !disabled
? () {
debugPrint("Button ${widget.control.id} clicked!");
if (url != "") {
openWebBrowser(url,
webWindowName: widget.control.attrString("urlTarget"));
}
server.sendPageEvent(
eventTarget: widget.control.id,
eventName: "click",
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/controls/floating_action_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../models/control.dart';
import '../utils/borders.dart';
import '../utils/colors.dart';
import '../utils/icons.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';
import 'error.dart';

Expand All @@ -28,6 +29,8 @@ class FloatingActionButtonControl extends StatelessWidget {

String? text = control.attrString("text");
IconData? icon = getMaterialIcon(control.attrString("icon", "")!);
String url = control.attrString("url", "")!;
String? urlTarget = control.attrString("urlTarget");
Color? bgColor = HexColor.fromString(
Theme.of(context), control.attrString("bgColor", "")!);
OutlinedBorder? shape = parseOutlinedBorder(control, "shape");
Expand All @@ -41,6 +44,9 @@ class FloatingActionButtonControl extends StatelessWidget {
? null
: () {
debugPrint("FloatingActionButtonControl ${control.id} clicked!");
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
FletAppServices.of(context).server.sendPageEvent(
eventTarget: control.id, eventName: "click", eventData: "");
};
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/controls/icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../models/control.dart';
import '../utils/buttons.dart';
import '../utils/colors.dart';
import '../utils/icons.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';
import 'error.dart';

Expand Down Expand Up @@ -69,12 +70,17 @@ class _IconButtonControlState extends State<IconButtonControl> {
var contentCtrls = widget.children.where((c) => c.name == "content");
bool autofocus = widget.control.attrBool("autofocus", false)!;
bool selected = widget.control.attrBool("selected", false)!;
String url = widget.control.attrString("url", "")!;
String? urlTarget = widget.control.attrString("urlTarget");
bool disabled = widget.control.isDisabled || widget.parentDisabled;

Function()? onPressed = disabled
? null
: () {
debugPrint("Button ${widget.control.id} clicked!");
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
FletAppServices.of(context).server.sendPageEvent(
eventTarget: widget.control.id,
eventName: "click",
Expand Down
20 changes: 14 additions & 6 deletions package/lib/src/controls/list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import '../flet_app_services.dart';
import '../models/control.dart';
import '../utils/edge_insets.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';

class ListTileControl extends StatelessWidget {
Expand Down Expand Up @@ -38,15 +39,22 @@ class ListTileControl extends StatelessWidget {
bool isThreeLine = control.attrBool("isThreeLine", false)!;
bool autofocus = control.attrBool("autofocus", false)!;
bool onclick = control.attrBool("onclick", false)!;
String url = control.attrString("url", "")!;
String? urlTarget = control.attrString("urlTarget");
bool disabled = control.isDisabled || parentDisabled;

Function()? onPressed = disabled || !onclick
? null
: () {
Function()? onPressed = (onclick || url != "") && !disabled
? () {
debugPrint("ListTile ${control.id} clicked!");
server.sendPageEvent(
eventTarget: control.id, eventName: "click", eventData: "");
};
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
if (onclick) {
server.sendPageEvent(
eventTarget: control.id, eventName: "click", eventData: "");
}
}
: null;

Function()? onLongPress = disabled
? null
Expand Down
7 changes: 7 additions & 0 deletions package/lib/src/controls/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../flet_app_services.dart';
import '../models/app_state.dart';
import '../models/control.dart';
import '../models/page_args_model.dart';
import '../utils/launch_url.dart';
import '../utils/text.dart';
import '../utils/uri.dart';
import 'create_control.dart';
Expand Down Expand Up @@ -49,6 +50,9 @@ class MarkdownControl extends StatelessWidget {
.bodyMedium!
.copyWith(fontFamily: "monospace"));

var autoFollowLinks = control.attrBool("autoFollowLinks", false)!;
var autoFollowLinksTarget = control.attrString("autoFollowLinksTarget");

return StoreConnector<AppState, PageArgsModel>(
distinct: true,
converter: (store) => PageArgsModel.fromStore(store),
Expand All @@ -67,6 +71,9 @@ class MarkdownControl extends StatelessWidget {
styleSheet: mdStyleSheet,
onTapLink: (String text, String? href, String title) {
debugPrint("Markdown link tapped ${control.id} clicked: $href");
if (autoFollowLinks && href != null) {
openWebBrowser(href, webWindowName: autoFollowLinksTarget);
}
FletAppServices.of(context).server.sendPageEvent(
eventTarget: control.id,
eventName: "tap_link",
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/controls/outlined_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../models/control.dart';
import '../utils/buttons.dart';
import '../utils/colors.dart';
import '../utils/icons.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';

class OutlinedButtonControl extends StatefulWidget {
Expand Down Expand Up @@ -61,6 +62,8 @@ class _OutlinedButtonControlState extends State<OutlinedButtonControl> {
Color? iconColor = HexColor.fromString(
Theme.of(context), widget.control.attrString("iconColor", "")!);
var contentCtrls = widget.children.where((c) => c.name == "content");
String url = widget.control.attrString("url", "")!;
String? urlTarget = widget.control.attrString("urlTarget");
bool onHover = widget.control.attrBool("onHover", false)!;
bool onLongPress = widget.control.attrBool("onLongPress", false)!;
bool autofocus = widget.control.attrBool("autofocus", false)!;
Expand All @@ -69,6 +72,9 @@ class _OutlinedButtonControlState extends State<OutlinedButtonControl> {
Function()? onPressed = !disabled
? () {
debugPrint("Button ${widget.control.id} clicked!");
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
server.sendPageEvent(
eventTarget: widget.control.id,
eventName: "click",
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/controls/text_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../models/control.dart';
import '../utils/buttons.dart';
import '../utils/colors.dart';
import '../utils/icons.dart';
import '../utils/launch_url.dart';
import 'create_control.dart';

class TextButtonControl extends StatefulWidget {
Expand Down Expand Up @@ -63,12 +64,17 @@ class _TextButtonControlState extends State<TextButtonControl> {
var contentCtrls = widget.children.where((c) => c.name == "content");
bool onHover = widget.control.attrBool("onHover", false)!;
bool onLongPress = widget.control.attrBool("onLongPress", false)!;
String url = widget.control.attrString("url", "")!;
String? urlTarget = widget.control.attrString("urlTarget");
bool autofocus = widget.control.attrBool("autofocus", false)!;
bool disabled = widget.control.isDisabled || widget.parentDisabled;

Function()? onPressed = !disabled
? () {
debugPrint("Button ${widget.control.id} clicked!");
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
server.sendPageEvent(
eventTarget: widget.control.id,
eventName: "click",
Expand Down
15 changes: 9 additions & 6 deletions package/lib/src/reducers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ AppState appReducer(AppState state, dynamic action) {
closeInAppWebView();
break;
case "launchUrl":
openWebBrowser(
action.payload.args["url"]!,
action.payload.args["web_window_name"],
action.payload.args["web_popup_window"]?.toLowerCase() == "true",
int.tryParse(action.payload.args["window_width"] ?? ""),
int.tryParse(action.payload.args["window_height"] ?? ""));
openWebBrowser(action.payload.args["url"]!,
webWindowName: action.payload.args["web_window_name"],
webPopupWindow:
action.payload.args["web_popup_window"]?.toLowerCase() ==
"true",
windowWidth:
int.tryParse(action.payload.args["window_width"] ?? ""),
windowHeight:
int.tryParse(action.payload.args["window_height"] ?? ""));
break;
case "canLaunchUrl":
canLaunchUrl(Uri.parse(action.payload.args["url"]!)).then((value) =>
Expand Down
9 changes: 6 additions & 3 deletions package/lib/src/utils/launch_url.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'package:url_launcher/url_launcher.dart';
import 'platform_utils_non_web.dart'
if (dart.library.js) "platform_utils_web.dart";

Future openWebBrowser(String url, String? webWindowName, bool webPopupWindow,
int? windowWidth, int? windowHeight) async {
if (webPopupWindow) {
Future openWebBrowser(String url,
{String? webWindowName,
bool? webPopupWindow,
int? windowWidth,
int? windowHeight}) async {
if (webPopupWindow == true) {
openPopupBrowserWindow(
url, webWindowName ?? "Flet", windowWidth ?? 1200, windowHeight ?? 800);
} else {
Expand Down
18 changes: 13 additions & 5 deletions package/lib/src/utils/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../utils/drawing.dart';
import '../utils/numbers.dart';
import '../utils/shadows.dart';
import 'colors.dart';
import 'launch_url.dart';

TextStyle? getTextStyle(BuildContext context, String styleName) {
var textTheme = Theme.of(context).textTheme;
Expand Down Expand Up @@ -91,21 +92,28 @@ InlineSpan? parseInlineSpan(ThemeData theme, ControlTreeViewModel spanViewModel,
if (spanViewModel.control.type == "textspan") {
bool disabled = spanViewModel.control.isDisabled || parentDisabled;
var onClick = spanViewModel.control.attrBool("onClick", false)!;
String url = spanViewModel.control.attrString("url", "")!;
String? urlTarget = spanViewModel.control.attrString("urlTarget");
return TextSpan(
text: spanViewModel.control.attrString("text"),
style: parseTextStyle(theme, spanViewModel.control, "style"),
children: parseTextSpans(theme, spanViewModel, parentDisabled, server),
mouseCursor: onClick && !disabled && server != null
? SystemMouseCursors.click
: null,
recognizer: onClick && !disabled && server != null
recognizer: (onClick || url != "") && !disabled && server != null
? (TapGestureRecognizer()
..onTap = () {
debugPrint("TextSpan ${spanViewModel.control.id} clicked!");
server.sendPageEvent(
eventTarget: spanViewModel.control.id,
eventName: "click",
eventData: "");
if (url != "") {
openWebBrowser(url, webWindowName: urlTarget);
}
if (onClick) {
server.sendPageEvent(
eventTarget: spanViewModel.control.id,
eventName: "click",
eventData: "");
}
})
: null,
onEnter: spanViewModel.control.attrBool("onEnter", false)! &&
Expand Down
Loading