agconnect_applinking 1.9.0+300 agconnect_applinking: ^1.9.0+300 copied to clipboard
App Linking allows you to create cross-platform links that can work as defined regardless of whether your app has been installed by a user.
/*
* Copyright 2020-2023. Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:async';
import 'package:agconnect_applinking/agconnect_applinking.dart';
import 'package:agconnect_applinking_example/widgets/custom_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MaterialApp(home: MyHomePage()));
class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final String _appTitle = 'HUAWEI AGC AppLinking';
final AGCAppLinking agcAppLinking = new AGCAppLinking();
StreamSubscription<ResolvedLinkData>? _streamSubscription;
void _showDialog(BuildContext context, String content) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: Key("dialog"),
title: Text("Result"),
content: Text(content),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.grey,
padding: EdgeInsets.all(0),
),
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
void _showDialogLong(BuildContext context, LongAppLinking content) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: Key("dialog"),
title: Text("Result"),
content: Text(content.toString()),
actions: <Widget>[
TextButton(
child: new Text("Copy LongLink"),
onPressed: () {
Clipboard.setData(
new ClipboardData(text: content.longLink.toString()));
}),
TextButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
void _showDialogShort(BuildContext context, ShortAppLinking content) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: Key("dialog"),
title: Text("Result"),
content: Text(content.toString()),
actions: <Widget>[
TextButton(
child: new Text("Copy ShortLink"),
onPressed: () {
Clipboard.setData(
new ClipboardData(text: content.shortLink.toString()));
}),
TextButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
void _handleException(dynamic e) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('ERROR'),
content: Text(
e is FormatException
? e.message
: AppLinkingException.from(e).message,
),
);
},
);
}
initState() {
super.initState();
_streamSubscription = agcAppLinking.onResolvedData!.listen((event) {
_showDialog(context, event.toString());
});
}
Future<void> _buildShortAppLinking(BuildContext context) async {
SocialCardInfo socialCardInfo = SocialCardInfo(
description: 'description short link',
imageUrl: 'https://avatars2.githubusercontent.com/u/64997192?s=200&v=4',
title: 'AGC Guides');
CampaignInfo campaignInfo =
CampaignInfo(medium: 'JULY', name: 'summer campaign', source: 'Huawei');
AndroidLinkInfo androidLinkInfo = new AndroidLinkInfo(
androidFallbackUrl:
'https://developer.huawei.com/consumer/en/doc/overview/AppGallery-connect',
androidOpenType:
AppLinkingAndroidLinkInfoAndroidOpenTypeConstants.CUSTOM_URL,
androidPackageName: "<package name>",
androidDeepLink:
'https://developer.huawei.com/consumer/en/doc/overview/AppGallery-connect');
iOSLinkInfo iosLinkInfo = iOSLinkInfo(
iosDeepLink: 'pages://flutter.huawei.applinking.com',
iosFallbackUrl:
'https://developer.huawei.com/consumer/en/doc/overview/AppGallery-connect',
iosBundleId: '<bundle id>');
ApplinkingInfo appLinkingInfo = ApplinkingInfo(
socialCardInfo: socialCardInfo,
androidLinkInfo: androidLinkInfo,
iosLinkInfo: iosLinkInfo,
domainUriPrefix: '<uri prefix>',
deepLink: 'https://developer.huawei.com',
campaignInfo: campaignInfo,
expireMinute: 5,
shortAppLinkingLength: ShortAppLinkingLengthConstants.SHORT);
try {
ShortAppLinking shortAppLinking =
await agcAppLinking.buildShortAppLinking(appLinkingInfo);
print(shortAppLinking.toString());
_showDialogShort(context, shortAppLinking);
} catch (e) {
_handleException(e);
}
}
Future<void> _buildLongAppLinking(BuildContext context) async {
SocialCardInfo socialCardInfo = SocialCardInfo(
description: 'description short link',
imageUrl: 'https://avatars2.githubusercontent.com/u/64997192?s=200&v=4',
title: 'AGC Guides');
CampaignInfo campaignInfo =
CampaignInfo(medium: 'JULY', name: 'summer campaign', source: 'Huawei');
AndroidLinkInfo androidLinkInfo = new AndroidLinkInfo(
androidFallbackUrl:
'https://developer.huawei.com/consumer/en/doc/overview/AppGallery-connect',
androidOpenType:
AppLinkingAndroidLinkInfoAndroidOpenTypeConstants.CUSTOM_URL,
androidPackageName: "<package name>",
androidDeepLink:
'https://developer.huawei.com/consumer/en/doc/overview/AppGallery-connect');
iOSLinkInfo iosLinkInfo = iOSLinkInfo(
iosDeepLink: 'pages://flutteraplinking.com',
iosFallbackUrl:
'https://developer.huawei.com/consumer/en/doc/overview/AppGallery-connect',
iosBundleId: '<bundle id>');
ApplinkingInfo appLinkingInfo = ApplinkingInfo(
socialCardInfo: socialCardInfo,
androidLinkInfo: androidLinkInfo,
iosLinkInfo: iosLinkInfo,
campaignInfo: campaignInfo,
domainUriPrefix: '<uri prefix>',
deepLink: 'https://developer.huawei.com');
try {
LongAppLinking longAppLinking =
await agcAppLinking.buildLongAppLinking(appLinkingInfo);
print(longAppLinking.longLink.toString());
_showDialogLong(context, longAppLinking);
} catch (e) {
_handleException(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_appTitle),
backgroundColor: Colors.blue,
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
CustomButton("buildAppLinking", _buildLongAppLinking),
CustomButton("buildShortLinking", _buildShortAppLinking)
],
),
),
);
}
}