初學者在這里
填充(填充:const EdgeInsets.all(8.0),子:容器(寬度:200,裝飾:BoxDecoration(影像:裝飾影像(
// ignore: prefer_const_constructors
image: AssetImage(
"assets/FeaturedPeoplePhotos/Top100/MostRecommendedBooks/1.png"),
fit: BoxFit.cover),
//color: Colors.amber,
borderRadius: BorderRadius.circular(8)),
),
),
我在 flutter 中撰寫了一個代碼,它顯示了特定書籍的影像,我想知道并需要一些關于如何在影像中添加附屬鏈接的幫助,以便用戶可以重定向到產品頁面,如亞馬遜或任何其他電子商務網站
uj5u.com熱心網友回復:
GestureDetector 是您正在尋找的小部件。它檢測其子部件上的各種手勢。在您的情況下,您可以使用 GestureDetector() 小部件包裝您的 Container() 小部件。像這樣:
GestureDetector(
onTap: (){
//here you can redirect to your affiliate link by using url_launcher
launch("www.yourlinkhere.com");
},
child: Container( //...your image container widget ),
),
launch() 函式由 url_launcher 包提供。您必須將其作為依賴項包含在 pubspec.yaml 檔案中。看看這個。
uj5u.com熱心網友回復:
您可以使用這個 url_launcher 包,并通過 onTap 事件從 GestureDetector 小部件呼叫。
https://pub.dev/packages/url_launcher
void launchAmazon() async {
const url = 'https://www.amazon.com/';
if (await canLaunch(Uri.encodeFull(url))) {
await launch(url);
} else {
throw ‘Could not launch $url’;
}
uj5u.com熱心網友回復:
您的問題有兩個部分首先如何使影像可點擊,您可以使用InkWell Widget或GestureDetector Widget它們都允許您點擊他們的子部件
第二部分是如何在Flutter應用程式中打開 URL,您可以使用名為url_launcher的包
下面是一個實作點擊圖片打開鏈接的demo
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() => runApp(const MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
const String _url = 'https://www.amazon.com/';
class _MyAppState extends State<MyApp> {
void _launchURL() async {
if (!await launch(_url)) throw 'Could not launch $_url';
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Expanded Column Sample'),
),
body: Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: _launchURL,
child: Container(
width: 200,
decoration: BoxDecoration(
image: DecorationImage(
// ignore: prefer_const_constructors
image: AssetImage("assets/FeaturedPeoplePhotos/Top100/MostRecommendedBooks/1.png"),
fit: BoxFit.cover),
//color: Colors.amber,
borderRadius: BorderRadius.circular(8)),
),
),
),
),
);
}
}
基本上我在這里所做的是用InkWell Widget包裝包含影像的Container Widget并使用onTap 中的包url_launcher中的函式啟動以在用戶單擊影像時打開 URL
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/400729.html
