solana_wallet_plugin 0.0.1
solana_wallet_plugin: ^0.0.1 copied to clipboard
A Flutter plugin that provides easy integration with Solana wallets for web applications. Supports multiple wallets including Phantom, Solflare, Slope, and more.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:solana_wallet_plugin/solana_wallet_plugin.dart';
import 'package:solana_wallet_plugin/widgets/wallet_selection_dialog.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Solana Wallet Plugin Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _walletPlugin = SolanaWalletPlugin();
String? _connectedWallet;
String? _walletAddress;
double? _balance;
bool _isLoading = false;
Future<void> _connectWallet() async {
if (!mounted) return;
final scaffoldMessenger = ScaffoldMessenger.of(context);
final navigator = Navigator.of(context);
showDialog(
context: context,
builder: (context) => WalletSelectionDialog(
walletPlugin: _walletPlugin,
onWalletSelected: (wallet) async {
try {
setState(() {
_isLoading = true;
});
final publicKey = await _walletPlugin.connectWallet();
final balance = await _walletPlugin.getBalance();
if (mounted) {
setState(() {
_connectedWallet = wallet;
_walletAddress = publicKey;
_balance = balance;
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
});
scaffoldMessenger.showSnackBar(
SnackBar(
content:
Text('Failed to connect to $wallet: ${e.toString()}'),
backgroundColor: Colors.red,
),
);
}
}
},
),
);
}
Future<void> _disconnectWallet() async {
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
setState(() {
_isLoading = true;
});
await _walletPlugin.disconnectWallet();
if (mounted) {
setState(() {
_connectedWallet = null;
_walletAddress = null;
_balance = null;
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
});
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text('Failed to disconnect: ${e.toString()}'),
backgroundColor: Colors.red,
),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Solana Wallet Plugin Demo'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_isLoading)
const CircularProgressIndicator()
else if (_connectedWallet == null)
ElevatedButton(
onPressed: _connectWallet,
child: const Text('Connect Wallet'),
)
else
Column(
children: [
Text(
'Connected to $_connectedWallet',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 16),
if (_walletAddress != null)
Text(
'Address: ${_walletAddress!.substring(0, 4)}...${_walletAddress!.substring(_walletAddress!.length - 4)}',
style: Theme.of(context).textTheme.bodyLarge,
),
if (_balance != null)
Text(
'Balance: ${_balance!.toStringAsFixed(4)} SOL',
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _disconnectWallet,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('Disconnect'),
),
],
),
],
),
),
),
);
}
}