Skip to content

Commit 402b001

Browse files
committed
added solana sample
1 parent c6e2225 commit 402b001

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import 'dart:convert';
2+
import 'dart:developer';
3+
4+
import 'package:flutter/material.dart';
5+
import 'package:walletconnect_flutter_dapp/utils/dart_defines.dart';
6+
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';
7+
import 'package:walletconnect_modal_flutter/walletconnect_modal_flutter.dart';
8+
9+
class SolanaSamplePage extends StatefulWidget {
10+
const SolanaSamplePage({super.key});
11+
12+
@override
13+
State<SolanaSamplePage> createState() => _MyHomePageState();
14+
}
15+
16+
class _MyHomePageState extends State<SolanaSamplePage> {
17+
bool _initialized = false;
18+
IWalletConnectModalService? _modalService;
19+
20+
@override
21+
void initState() {
22+
super.initState();
23+
initialize();
24+
}
25+
26+
Future<void> initialize() async {
27+
_modalService = WalletConnectModalService(
28+
projectId: DartDefines.projectId,
29+
metadata: const PairingMetadata(
30+
name: 'Flutter Dapp Example',
31+
description: 'Flutter Dapp Example',
32+
url: 'https://www.walletconnect.com/',
33+
icons: ['https://walletconnect.com/walletconnect-logo.png'],
34+
redirect: Redirect(
35+
native: 'flutterdapp://',
36+
universal: 'https://www.walletconnect.com',
37+
),
38+
),
39+
requiredNamespaces: {
40+
'eip155': const RequiredNamespace(
41+
chains: ['eip155:1'],
42+
methods: [
43+
'personal_sign',
44+
'eth_sendTransaction',
45+
],
46+
events: [
47+
'chainChanged',
48+
'accountsChanged',
49+
],
50+
),
51+
},
52+
optionalNamespaces: {
53+
'solana': const RequiredNamespace(
54+
chains: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'],
55+
methods: [
56+
'solana_signMessage',
57+
'solana_signTransaction',
58+
],
59+
events: [],
60+
),
61+
},
62+
);
63+
64+
await _modalService!.init();
65+
66+
_modalService!.web3App!.onSessionConnect.subscribe(_onSessionConnect);
67+
_modalService!.web3App!.onSessionDelete.subscribe(_onSessionDisonnect);
68+
_modalService!.web3App!.onSessionEvent.subscribe(_onSessionEvent);
69+
70+
setState(() => _initialized = true);
71+
}
72+
73+
void _onSessionConnect(SessionConnect? args) {
74+
log('_onSessionConnect ${jsonEncode(args?.session.toJson())}');
75+
}
76+
77+
void _onSessionDisonnect(SessionDelete? args) {
78+
log('_onSessionDisonnect ${args.toString()}');
79+
}
80+
81+
void _onSessionEvent(SessionEvent? args) {
82+
log('_onSessionEvent ${args.toString()}');
83+
}
84+
85+
@override
86+
void dispose() {
87+
_modalService!.web3App!.onSessionConnect.unsubscribe(_onSessionConnect);
88+
_modalService!.web3App!.onSessionDelete.unsubscribe(_onSessionDisonnect);
89+
_modalService!.web3App!.onSessionEvent.unsubscribe(_onSessionEvent);
90+
super.dispose();
91+
}
92+
93+
@override
94+
Widget build(BuildContext context) {
95+
if (!_initialized) {
96+
return Center(
97+
child: CircularProgressIndicator(
98+
color: WalletConnectModalTheme.getData(context).primary100,
99+
),
100+
);
101+
}
102+
103+
return Scaffold(
104+
appBar: AppBar(
105+
title: const Text('WalletConnectModal'),
106+
),
107+
body: Center(
108+
child: WalletConnectModalConnect(
109+
service: _modalService!,
110+
),
111+
),
112+
);
113+
}
114+
}

0 commit comments

Comments
 (0)