To integrate thePhantom Walletinto your Flutter web app, you can leverage the officialPhantom documentationand JavaScript API. Below is a step-by-step guide to integrating Phantom Wallet with your Flutter web app:
- Understand the Phantom Wallet API
The Phantom Wallet API enables interaction with the blockchain, allowing your app to perform tasks like connecting wallets, signing transactions, and more.
Phantom Wallet useswindow.solanain the browser to interact with its functionality. Since Flutter web apps run on the browser, you'll need to integrate JavaScript into your Flutter project to access Phantom's API.
- Prerequisites
Ensure your Flutter web app is set up and deployed properly.
Add support for interop with JavaScript usingdart:jsorjspackage.
Ensure your Flutter web app is set up and deployed properly.
Add support for interop with JavaScript usingdart:jsorjspackage.
- Steps to Integrate Phantom Wallet
Step 1: Detect Phantom Wallet
Phantom Wallet exposeswindow.solanain the browser. Use this to check if Phantom is installed. You can use JavaScript interop to communicate with the browser.
Add the following JavaScript snippet to check if Phantom Wallet is available:
────────────────────────────────────────────────────────────────────
javascriptCopyif (window.solana && window.solana.isPhantom) {
console.log("Phantom Wallet is installed.");
} else {
console.log("Phantom Wallet is not installed.");
}
Step 2: Add JavaScript in Flutter
You can either use thedart:htmlpackage for JS interop or include a JavaScript file in your project.
Here’s how to call JavaScript from Flutter:
Add a filephantom_wallet.jsin yourwebfolder with the following code:javascriptCopyfunctionisPhantomInstalled() {returnwindow.solana&&window.solana.isPhantom;
}asyncfunctionconnectPhantom() {try{constresp =awaitwindow.solana.connect();returnresp.publicKey.toString();// Returns the wallet's public key}catch(err) {console.error("Connection failed:", err);returnnull;
}
}
Include the file in your /index.html:htmlCopy<scriptsrc="phantom_wallet.js">
Add a filephantom_wallet.jsin yourwebfolder with the following code:
────────────────────────────────────────────────────────────────────
javascriptCopyfunction isPhantomInstalled() {
return window.solana && window.solana.isPhantom;
}
async function connectPhantom() {
try {
const resp = await window.solana.connect();
return resp.publicKey.toString(); // Returns the wallet's public key
} catch (err) {
console.error("Connection failed:", err);
return null;
}
}
Include the file in your /index.html:
────────────────────────────────────────────────────────────────────
htmlCopy
Step 3: Call JavaScript from Flutter
Usedart:jsor thejspackage to call these JavaScript functions from Flutter.
Here’s an example using thedart:jspackage:
Add thejspackage to yourpubspec.yaml:yamlCopydependencies:js:^0.6.5
Create a Dart interface for the JS functions:dartCopy@JS('isPhantomInstalled')
external bool isPhantomInstalled();
@JS('connectPhantom')
external String? connectPhantom();
Use the functions in your Flutter app:dartCopyimport 'dart:js' as js;
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Phantom Wallet Integration")),
body: Center(
child: PhantomIntegration(),
),
),
);
}
}
class PhantomIntegration extends StatefulWidget {
@override
_PhantomIntegrationState createState() => _();
}
class _ extends State {
String? _walletAddress;
void _connectWallet() {
if (js.context.callMethod('isPhantomInstalled')) {
final walletAddress = js.context.callMethod('connectPhantom');
setState(() {
_walletAddress = walletAddress;
});
} else {
setState(() {
_walletAddress = "Phantom Wallet not installed.";
});
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_walletAddress ?? "No Wallet Connected"),
SizedBox(height: 20),
ElevatedButton(
onPressed: _connectWallet,
child: Text("Connect Phantom Wallet"),
),
],
);
}
}
Add thejspackage to yourpubspec.yaml:
────────────────────────────────────────────────────────────────────
yamlCopydependencies:
js: ^0.6.5
Create a Dart interface for the JS functions:
────────────────────────────────────────────────────────────────────
dartCopy@JS('isPhantomInstalled')
external bool isPhantomInstalled();
@JS('connectPhantom')
external String? connectPhantom();
Use the functions in your Flutter app:
────────────────────────────────────────────────────────────────────
dartCopyimport 'dart:js' as js;
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Phantom Wallet Integration")),
body: Center(
child: PhantomIntegration(),
),
),
);
}
}
class PhantomIntegration extends StatefulWidget {
@override
_PhantomIntegrationState createState() => _();
}
class _ extends State {
String? _walletAddress;
void _connectWallet() {
if (js.context.callMethod('isPhantomInstalled')) {
final walletAddress = js.context.callMethod('connectPhantom');
setState(() {
_walletAddress = walletAddress;
});
} else {
setState(() {
_walletAddress = "Phantom Wallet not installed.";
});
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_walletAddress ?? "No Wallet Connected"),
SizedBox(height: 20),
ElevatedButton(
onPressed: _connectWallet,
child: Text("Connect Phantom Wallet"),
),
],
);
}
}
Step 4: Handle Transactions
Once connected, you can interact with the wallet to sign transactions or perform actions. Phantom Wallet allows you to send transactions through:
window.solana.signTransaction
window.solana.sendTransaction
window.solana.signTransaction
window.solana.sendTransaction
Extend yourphantom_wallet.jsto include methods for signing or sending transactions. Example:
────────────────────────────────────────────────────────────────────
javascriptCopyasync function signTransaction(transaction) {
const signedTransaction = await window.solana.signTransaction(transaction);
return signedTransaction;
}
You can invoke this from Dart using the samedart:.
- Test and Deploy
Test your app in a browser with Phantom Wallet installed.
Ensure proper error handling if Phantom Wallet isn’t installed.
Test your app in a browser with Phantom Wallet installed.
Ensure proper error handling if Phantom Wallet isn’t installed.
Would you like help with a complete example for signing transactions or additional features like fetching balance?