Skip to content

Commit

Permalink
Merge pull request #46 from WalletConnect/feat/wallet-example
Browse files Browse the repository at this point in the history
Feat/wallet example
  • Loading branch information
Luzzotica authored Mar 8, 2023
2 parents d636cbd + 390406a commit 13de9c4
Show file tree
Hide file tree
Showing 19 changed files with 931 additions and 66 deletions.
194 changes: 130 additions & 64 deletions example/wallet/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';
import 'package:walletconnect_flutter_v2_wallet/models/page_data.dart';
import 'package:walletconnect_flutter_v2_wallet/utils/constants.dart';
import 'package:flutter/material.dart';
import 'package:walletconnect_flutter_v2_wallet/utils/string_constants.dart';

void main() {
runApp(const MyApp());
Expand All @@ -11,96 +15,158 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
title: StringConstants.appTitle,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
home: const MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;
const MyHomePage({super.key});

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool _initializing = true;

Web3App? _web3App;

List<PageData> _pageDatas = [];
int _selectedIndex = 0;

// SessionData? _selectedSession;
// List<SessionData> _allSessions = [];
// List<PairingInfo> _allPairings = [];

@override
void initState() {
initialize();
super.initState();
}

Future<void> initialize() async {
// try {
_web3App = await Web3App.createInstance(
projectId: Constants.projectId,
metadata: const PairingMetadata(
name: 'Example Dapp',
description: 'Example Dapp',
url: 'https://walletconnect.com/',
icons: ['https://walletconnect.com/walletconnect-logo.png'],
),
);

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
// _pageDatas = [
// PageData(
// page: ConnectPage(web3App: _web3App!),
// title: StringConstants.connectPageTitle,
// icon: Icons.home,
// ),
// PageData(
// page: PairingsPage(web3App: _web3App!),
// title: StringConstants.pairingsPageTitle,
// icon: Icons.connect_without_contact_sharp,
// ),
// PageData(
// page: SessionsPage(web3App: _web3App!),
// title: StringConstants.sessionsPageTitle,
// icon: Icons.confirmation_number_outlined,
// ),
// PageData(
// page: AuthPage(web3App: _web3App!),
// title: StringConstants.authPageTitle,
// icon: Icons.lock,
// ),
// ];

_initializing = false;
});
// } on WalletConnectError catch (e) {
// print(e.message);
// }
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
if (_initializing) {
return const Center(
child: CircularProgressIndicator(
color: StyleConstants.primaryColor,
),
);
}

final List<Widget> navRail = [];
if (MediaQuery.of(context).size.width >= Constants.smallScreen) {
navRail.add(_buildNavigationRail());
}
navRail.add(
Expanded(
child: _pageDatas[_selectedIndex].page,
),
);

return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
title: Text(_pageDatas[_selectedIndex].title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
bottomNavigationBar:
MediaQuery.of(context).size.width < Constants.smallScreen
? _buildBottomNavBar()
: null,
body: Row(
mainAxisSize: MainAxisSize.max,
children: navRail,
),
);
}

Widget _buildBottomNavBar() {
return BottomNavigationBar(
currentIndex: _selectedIndex,
unselectedItemColor: Colors.grey,
selectedItemColor: Colors.indigoAccent,
// called when one tab is selected
onTap: (int index) {
setState(() {
_selectedIndex = index;
});
},
// bottom tab items
items: _pageDatas
.map(
(e) => BottomNavigationBarItem(
icon: Icon(e.icon),
label: e.title,
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
)
.toList(),
);
}

Widget _buildNavigationRail() {
return NavigationRail(
selectedIndex: _selectedIndex,
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.selected,
destinations: _pageDatas
.map(
(e) => NavigationRailDestination(
icon: Icon(e.icon),
label: Text(e.title),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
)
.toList(),
);
}
}
76 changes: 76 additions & 0 deletions example/wallet/lib/models/accounts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:flutter/foundation.dart';

class AccountDetails {
final String address;
final String chain;

const AccountDetails({
required this.address,
required this.chain,
});

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is AccountDetails &&
other.address == address &&
other.chain == chain;
}

@override
int get hashCode => address.hashCode ^ chain.hashCode;
}

class Account {
final int id;
final String name;
final String mnemonic;
final String privateKey;
final List<AccountDetails> details;

const Account({
required this.id,
required this.name,
required this.mnemonic,
required this.privateKey,
required this.details,
});

Account copyWith({
int? id,
String? name,
String? mnemonic,
String? privateKey,
List<AccountDetails>? details,
}) {
return Account(
id: id ?? this.id,
name: name ?? this.name,
mnemonic: mnemonic ?? this.mnemonic,
privateKey: privateKey ?? this.privateKey,
details: details ?? this.details,
);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is Account &&
other.id == id &&
other.name == name &&
other.mnemonic == mnemonic &&
other.privateKey == privateKey &&
listEquals(other.details, details);
}

@override
int get hashCode {
return id.hashCode ^
name.hashCode ^
mnemonic.hashCode ^
privateKey.hashCode ^
details.hashCode;
}
}
49 changes: 49 additions & 0 deletions example/wallet/lib/models/chain_metadata.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

enum ChainType {
eip155,
solana,
kadena,
}

class ChainMetadata {
final String chainId;
final String name;
final String logo;
final bool isTestnet;
final Color color;
final ChainType type;
final List<String> rpc;

const ChainMetadata({
required this.chainId,
required this.name,
required this.logo,
this.isTestnet = false,
required this.color,
required this.type,
required this.rpc,
});

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is ChainMetadata &&
other.chainId == chainId &&
other.name == name &&
other.logo == logo &&
other.isTestnet == isTestnet &&
listEquals(other.rpc, rpc);
}

@override
int get hashCode {
return chainId.hashCode ^
name.hashCode ^
logo.hashCode ^
rpc.hashCode ^
isTestnet.hashCode;
}
}
19 changes: 19 additions & 0 deletions example/wallet/lib/models/eth/ethereum_sign_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum WCSignType {
message,
personalMessage,
typedMessageV2,
typedMessageV3,
typedMessageV4,
}

class EthereumSignMessage {
final String data;
final String address;
final WCSignType type;

const EthereumSignMessage({
required this.data,
required this.address,
required this.type,
});
}
Loading

0 comments on commit 13de9c4

Please sign in to comment.