/
widgets.dart
116 lines (102 loc) · 3.44 KB
/
widgets.dart
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
import 'package:commercio_ui/commercio_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
// The UI widgets layer is the topmost layer in the CommercioUI library.
// The layer provides a set of pre-built widgets around the BLoC components
// to build applications at maximun speed.
//
// The supported widgets automatically send events and elaborate the response
// states.
// - Custom widgets that eliminate BLoC boilerplate
// - Default error handling
void main() {
runApp(
RepositoryProvider<StatefulCommercioAccount>(
create: (_) => StatefulCommercioAccount(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
// Provide the CommercioAccountBloc, still required
return BlocProvider<CommercioAccountGenerateWalletBloc>(
create: (_) => CommercioAccountGenerateWalletBloc(
commercioAccount:
RepositoryProvider.of<StatefulCommercioAccount>(context),
),
child: MaterialApp(
title: 'Example App',
home: ExamplePage(),
),
);
}
}
class ExamplePage extends StatefulWidget {
ExamplePage({Key key}) : super(key: key);
@override
_ExamplePageState createState() => _ExamplePageState();
}
class _ExamplePageState extends State<ExamplePage> {
final TextEditingController textCtrl = TextEditingController();
@override
void dispose() {
textCtrl.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Example'),
),
body: Column(
children: [
// Custom FlatButton that when is pressed sends an event
// to generate new mnemonic and derive the wallet
GenerateWalletFlatButton(
event: () => const CommercioAccountGenerateWalletEvent(),
disabledTextColor: Colors.brown,
color: Colors.orangeAccent,
disabledColor: Colors.orange[700],
child: (_) => const Text('Generate new wallet'),
),
const Text('Mnemonic words:'),
// We can still use the BLoC components to intercept the states
// and provide custom logic
BlocBuilder<CommercioAccountGenerateWalletBloc,
CommercioAccountGenerateWalletState>(
builder: (_, state) {
state.when(
(mnemonic, wallet, walletAddress) => textCtrl.text = mnemonic,
initial: () => textCtrl.text = '',
loading: () => textCtrl.text = 'Generating...',
error: (e) => textCtrl.text = 'Error: $e',
);
return TextField(
controller: textCtrl,
readOnly: true,
style: TextStyle(
color: (state is CommercioAccountGenerateWalletLoading)
? Colors.grey
: Colors.black,
),
maxLines: null,
);
},
),
const Text('Wallet address:'),
// Custom TextField that receives a wallet-generation state
// that the user can intercept in the callback
GenerateWalletTextField(
readOnly: true,
loading: (_) => 'Generating...',
text: (_, state) => state.walletAddress,
),
],
),
);
}
}