193 lines
5.2 KiB
Dart
193 lines
5.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
import 'package:qr_flutter/qr_flutter.dart';
|
|
import 'package:truncate/truncate.dart';
|
|
|
|
late Box addressBox;
|
|
List profiles = [];
|
|
|
|
Future<void> main() async {
|
|
await Hive.initFlutter();
|
|
addressBox = await Hive.openBox("addressBox");
|
|
// await addressBox.clear();
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'ĞDev annuaire',
|
|
theme: ThemeData(
|
|
primarySwatch: Colors.blue,
|
|
),
|
|
home: const MyHomePage(title: 'ĞDev annuaire'),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({Key? key, required this.title}) : super(key: key);
|
|
|
|
final String title;
|
|
|
|
@override
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
refresh() {
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
profiles.clear();
|
|
addressBox.toMap().forEach((key, value) {
|
|
profiles.add(value);
|
|
});
|
|
|
|
return Scaffold(
|
|
backgroundColor: Colors.orange[200],
|
|
body: profileTiles(context, profiles, refresh),
|
|
);
|
|
}
|
|
}
|
|
|
|
Widget profileTiles(BuildContext context, List profiles, refresh) {
|
|
final double screenWidth = MediaQuery.of(context).size.width;
|
|
int nbrColumn = 9;
|
|
TextEditingController nameController = TextEditingController();
|
|
TextEditingController addressController = TextEditingController();
|
|
|
|
if (screenWidth <= 600) {
|
|
nbrColumn = 1;
|
|
} else if (screenWidth <= 900) {
|
|
nbrColumn = 2;
|
|
} else if (screenWidth <= 1200) {
|
|
nbrColumn = 3;
|
|
} else if (screenWidth <= 1500) {
|
|
nbrColumn = 4;
|
|
} else if (screenWidth <= 1800) {
|
|
nbrColumn = 5;
|
|
} else if (screenWidth <= 2100) {
|
|
nbrColumn = 6;
|
|
} else if (screenWidth <= 2400) {
|
|
nbrColumn = 7;
|
|
} else if (screenWidth <= 2700) {
|
|
nbrColumn = 8;
|
|
}
|
|
|
|
return CustomScrollView(slivers: <Widget>[
|
|
const SliverToBoxAdapter(child: SizedBox(height: 20)),
|
|
SliverToBoxAdapter(
|
|
child: Row(
|
|
children: [
|
|
const SizedBox(width: 20),
|
|
SizedBox(
|
|
width: 150,
|
|
child: TextField(
|
|
controller: nameController,
|
|
decoration: const InputDecoration(
|
|
hintText: "Nom",
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 20),
|
|
SizedBox(
|
|
width: 470,
|
|
child: TextField(
|
|
controller: addressController,
|
|
decoration: const InputDecoration(
|
|
hintText: "Adresse",
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 20),
|
|
IconButton(
|
|
icon: const Icon(Icons.add_reaction_outlined),
|
|
onPressed: (() async {
|
|
await addProfile(nameController.text, addressController.text);
|
|
refresh();
|
|
}),
|
|
),
|
|
],
|
|
)),
|
|
SliverGrid.count(
|
|
key: const Key('listWallets'),
|
|
crossAxisCount: nbrColumn,
|
|
childAspectRatio: 1,
|
|
crossAxisSpacing: 0,
|
|
mainAxisSpacing: 0,
|
|
children: <Widget>[
|
|
for (String profile in profiles)
|
|
Container(
|
|
color: Colors.orange[200],
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(10),
|
|
child: Container(
|
|
color: Colors.orange[100],
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(10),
|
|
child: tile(
|
|
context, profile.split(':')[0], profile.split(':')[1]),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
Widget tile(BuildContext context, String name, String address) {
|
|
return Container(
|
|
color: Colors.orange[50],
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
Text(name),
|
|
const SizedBox(height: 3),
|
|
InkWell(
|
|
onTap: () => snackCopyKey(context, address),
|
|
child: Text(getShortPubkey(address)),
|
|
),
|
|
QrImageWidget(
|
|
// gapless: false,
|
|
data: address,
|
|
version: QrVersions.auto,
|
|
size: 200,
|
|
),
|
|
],
|
|
)),
|
|
);
|
|
}
|
|
|
|
String getShortPubkey(String pubkey) {
|
|
String pubkeyShort = truncate(pubkey, 7,
|
|
omission: String.fromCharCode(0x2026),
|
|
position: TruncatePosition.end) +
|
|
truncate(pubkey, 6, omission: "", position: TruncatePosition.start);
|
|
return pubkeyShort;
|
|
}
|
|
|
|
snackCopyKey(BuildContext context, String address) {
|
|
// _copyToClipboardHack(address);
|
|
Clipboard.setData(ClipboardData(text: address));
|
|
|
|
const snackBar = SnackBar(
|
|
padding: EdgeInsets.all(20),
|
|
content: Text("Cette adresse a été copié dans votre presse-papier.",
|
|
style: TextStyle(fontSize: 16)),
|
|
duration: Duration(seconds: 2));
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
}
|
|
|
|
Future addProfile(String name, String address) async {
|
|
await addressBox.add('$name:$address');
|
|
}
|