-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathhome_page.dart
More file actions
196 lines (174 loc) · 5.69 KB
/
home_page.dart
File metadata and controls
196 lines (174 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import 'package:flutter/material.dart';
import 'package:web3modal_flutter/web3modal_flutter.dart';
import 'package:walletconnect_flutter_dapp/widgets/session_widget.dart';
import 'package:walletconnect_flutter_dapp/utils/dart_defines.dart';
import 'package:walletconnect_flutter_dapp/utils/string_constants.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
required this.swapTheme,
required this.changeTheme,
});
final VoidCallback swapTheme;
final VoidCallback changeTheme;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late W3MService _w3mService;
bool _initialized = false;
@override
void initState() {
super.initState();
_initializeService();
}
void _initializeService() async {
// See https://docs.walletconnect.com/web3modal/flutter/custom-chains
W3MChainPresets.chains.putIfAbsent('42220', () => _exampleCustomChain);
_w3mService = W3MService(
projectId: DartDefines.projectId,
logLevel: LogLevel.error,
metadata: const PairingMetadata(
name: StringConstants.w3mPageTitleV3,
description: StringConstants.w3mPageTitleV3,
url: 'https://www.walletconnect.com/',
icons: ['https://web3modal.com/images/rpc-illustration.png'],
redirect: Redirect(
native: 'flutterdapp://',
universal: 'https://www.walletconnect.com',
),
),
// requiredNamespaces: {},
// optionalNamespaces: {},
// featuredWalletIds: {},
// includedWalletIds: {},
// excludedWalletIds: {},
);
await _w3mService.init();
// If you want to support just one chain uncomment this line and avoid using W3MNetworkSelectButton()
// _w3mService.selectChain(W3MChainPresets.chains['137']);
_w3mService.addListener(_serviceListener);
_w3mService.web3App?.onSessionEvent.subscribe(_onSessionEvent);
_w3mService.web3App?.onSessionConnect.subscribe(_onSessionConnect);
_w3mService.web3App?.onSessionDelete.subscribe(_onSessionDelete);
setState(() => _initialized = true);
}
@override
void dispose() {
_w3mService.web3App?.onSessionEvent.unsubscribe(_onSessionEvent);
_w3mService.web3App?.onSessionConnect.unsubscribe(_onSessionConnect);
_w3mService.web3App?.onSessionDelete.unsubscribe(_onSessionDelete);
super.dispose();
}
void _serviceListener() {
setState(() {});
}
void _onSessionEvent(SessionEvent? args) {
debugPrint('[$runtimeType] _onSessionEvent $args');
}
void _onSessionConnect(SessionConnect? args) {
debugPrint('[$runtimeType] _onSessionConnect $args');
}
void _onSessionDelete(SessionDelete? args) {
debugPrint('[$runtimeType] _onSessionDelete $args');
}
@override
Widget build(BuildContext context) {
final isSquare = Web3ModalTheme.radiusesOf(context).isSquare();
return Scaffold(
backgroundColor: Web3ModalTheme.colorsOf(context).background300,
appBar: AppBar(
elevation: 0.0,
title: const Text(StringConstants.w3mPageTitleV3),
backgroundColor: Web3ModalTheme.colorsOf(context).background100,
foregroundColor: Web3ModalTheme.colorsOf(context).foreground100,
actions: [
IconButton(
icon: isSquare
? const Icon(Icons.yard_outlined)
: const Icon(Icons.yard),
onPressed: widget.changeTheme,
),
IconButton(
icon: Web3ModalTheme.maybeOf(context)?.isDarkMode ?? false
? const Icon(Icons.light_mode)
: const Icon(Icons.dark_mode),
onPressed: widget.swapTheme,
),
],
),
body: Builder(builder: (context) {
if (!_initialized) {
return Center(
child: CircularProgressIndicator(
color: Web3ModalTheme.colorsOf(context).accent100,
),
);
}
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
_ButtonsView(w3mService: _w3mService),
const Divider(height: 0.0),
_ConnectedView(w3mService: _w3mService)
],
),
);
}),
);
}
}
class _ButtonsView extends StatelessWidget {
const _ButtonsView({required this.w3mService});
final W3MService w3mService;
@override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox.square(dimension: 8.0),
Visibility(
visible: !w3mService.isConnected,
child: W3MNetworkSelectButton(service: w3mService),
),
W3MConnectWalletButton(service: w3mService),
const SizedBox.square(dimension: 8.0),
],
);
}
}
class _ConnectedView extends StatelessWidget {
const _ConnectedView({required this.w3mService});
final W3MService w3mService;
@override
Widget build(BuildContext context) {
if (!w3mService.isConnected) {
return const SizedBox.shrink();
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox.square(dimension: 12.0),
W3MAccountButton(service: w3mService),
SessionWidget(
w3mService: w3mService,
launchRedirect: () {
w3mService.launchConnectedWallet();
},
),
const SizedBox.square(dimension: 12.0),
],
);
}
}
final _exampleCustomChain = W3MChainInfo(
chainName: 'Celo',
namespace: 'eip155:42220',
chainId: '42220',
tokenName: 'CELO',
rpcUrl: 'https://forno.celo.org/',
blockExplorer: W3MBlockExplorer(
name: 'Celo Explorer',
url: 'https://explorer.celo.org/mainnet',
),
);