Created
March 11, 2024 18:40
-
-
Save quetool/ccc195b75a44eaa21929c4beeddc75cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:web3modal_flutter/web3modal_flutter.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
W3MService? _w3mService; | |
@override | |
void initState() { | |
super.initState(); | |
WidgetsBinding.instance.addPostFrameCallback((_) async { | |
await _initialize(); | |
await _initialize(); | |
}); | |
} | |
Future<void> _initialize() async { | |
try { | |
if (_w3mService?.status.isInitialized ?? false) { | |
return; | |
} | |
_w3mService?.dispose(); | |
_w3mService = W3MService( | |
projectId: 'cad4956f31a5e40a00b62865b030c6f8', | |
metadata: const PairingMetadata( | |
name: 'Web3Modal Flutter Example', | |
description: 'Web3Modal Flutter Example', | |
url: 'https://web3modal.com/', | |
icons: [ | |
'https://docs.walletconnect.com/assets/images/web3modalLogo-2cee77e07851ba0a710b56d03d4d09dd.png' | |
], | |
redirect: Redirect( | |
native: 'flutterdapp://', | |
universal: 'https://web3modal.com', | |
), | |
), | |
featuredWalletIds: { | |
'19177a98252e07ddfc9af2083ba8e07ef627cb6103467ffebb3f8f4205fd7927', // Ledger Live | |
'4457c130df49fb3cb1f8b99574b97b35208bd3d0d13b8d25d2b5884ed2cad13a', // Shapeshift | |
'fd20dc426fb37566d803205b19bbc1d4096b248ac04548e3cfb6b3a38bd033aa', // Coinbase Wallet | |
'38f5d18bd8522c244bdd70cb4a68e0e718865155811c043f052fb9f1c51de662', // Bitget | |
}, | |
); | |
_w3mService!.addListener(_serviceListener); | |
_w3mService!.onModalConnect.subscribe(_onModalConnect); | |
// _w3mService?.onModalDisconnect.subscribe(_onModalConnect); | |
// _w3mService?.onModalError.subscribe(_onModalConnect); | |
await _w3mService!.init(); | |
// await _w3mService!.init(); | |
} catch (e) { | |
debugPrint('[$runtimeType] init error $e'); | |
} | |
} | |
@override | |
void dispose() { | |
// _w3mService?.onSessionConnectEvent.unsubscribe(_onSessionConnect); | |
// _w3mService?.onCoinbaseConnect.unsubscribe(_onCoinbaseConnect); | |
_w3mService?.onModalConnect.unsubscribe(_onModalConnect); | |
super.dispose(); | |
} | |
void _onModalConnect(ModalConnect? args) { | |
final address = args?.session.address; | |
debugPrint('[$runtimeType] _onCoinbaseConnect $address}'); | |
} | |
void _serviceListener() { | |
debugPrint('[$runtimeType] _serviceListener'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final isConnected = _w3mService?.isConnected ?? false; | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Connected: ${_w3mService?.session?.connectedWalletName}', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
const SizedBox(height: 10.0), | |
Visibility( | |
visible: isConnected, | |
child: ElevatedButton( | |
onPressed: () async { | |
final cid = _w3mService?.selectedChain?.chainId ?? 1; | |
_w3mService?.launchConnectedWallet(); | |
final response = await _w3mService?.request( | |
topic: _w3mService?.session?.topic ?? '', | |
chainId: 'eip155:$cid', | |
request: SessionRequestParams( | |
method: 'personal_sign', | |
params: [ | |
'Hello world!', | |
_w3mService?.session?.address, | |
], | |
), | |
); | |
debugPrint(response); | |
}, | |
child: const Text('personal_sign'), | |
), | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
_w3mService?.openModal(context); | |
}, | |
child: const Icon( | |
Icons.change_circle_rounded, | |
size: 30.0, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment