stack_wallet/lib/widgets/hover_text_field.dart

156 lines
3.9 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-12-05 22:36:28 +00:00
import 'dart:async';
2022-10-30 17:13:32 +00:00
import 'package:flutter/material.dart';
2022-12-05 22:36:28 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../notifications/show_flush_bar.dart';
import '../providers/db/main_db_provider.dart';
import '../providers/global/wallets_provider.dart';
import '../themes/stack_colors.dart';
import '../utilities/constants.dart';
import '../utilities/text_styles.dart';
import '../utilities/util.dart';
2022-10-30 17:13:32 +00:00
2022-12-05 22:36:28 +00:00
class DesktopWalletNameField extends ConsumerStatefulWidget {
const DesktopWalletNameField({
2024-05-27 23:56:22 +00:00
super.key,
2022-12-05 22:36:28 +00:00
required this.walletId,
2024-05-27 23:56:22 +00:00
});
2022-10-30 17:13:32 +00:00
2022-12-05 22:36:28 +00:00
final String walletId;
2022-10-30 17:13:32 +00:00
@override
2022-12-05 22:36:28 +00:00
ConsumerState<DesktopWalletNameField> createState() => _HoverTextFieldState();
2022-10-30 17:13:32 +00:00
}
2022-12-05 22:36:28 +00:00
class _HoverTextFieldState extends ConsumerState<DesktopWalletNameField> {
late final TextEditingController controller;
late final FocusNode focusNode;
bool readOnly = true;
2022-10-30 17:13:32 +00:00
final InputBorder inputBorder = OutlineInputBorder(
borderSide: const BorderSide(
width: 0,
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(Constants.size.circularBorderRadius),
);
2022-12-05 22:36:28 +00:00
Future<void> onDone() async {
final info = ref.read(pWallets).getWallet(widget.walletId).info;
final currentWalletName = info.name;
2022-12-05 22:36:28 +00:00
final newName = controller.text;
String? errMessage;
try {
await info.updateName(
newName: newName,
isar: ref.read(mainDBProvider).isar,
);
} catch (e) {
if (e.toString().contains("Empty wallet name not allowed!")) {
errMessage = "Empty wallet name not allowed.";
} else {
errMessage = e.toString();
}
}
if (mounted) {
if (errMessage == null) {
2022-12-05 22:36:28 +00:00
unawaited(
showFloatingFlushBar(
type: FlushBarType.success,
message: "Wallet renamed",
context: context,
),
);
} else {
unawaited(
showFloatingFlushBar(
type: FlushBarType.warning,
message: "Wallet named \"$newName\" already exists",
context: context,
),
);
controller.text = currentWalletName;
}
}
}
void listenerFunc() {
if (!focusNode.hasPrimaryFocus && !readOnly) {
setState(() {
readOnly = true;
});
onDone.call();
}
}
2022-10-30 17:13:32 +00:00
@override
void initState() {
2022-12-05 22:36:28 +00:00
controller = TextEditingController();
focusNode = FocusNode();
2022-10-30 17:13:32 +00:00
2022-12-05 22:36:28 +00:00
focusNode.addListener(listenerFunc);
WidgetsBinding.instance.addPostFrameCallback((_) {
controller.text = ref.read(pWallets).getWallet(widget.walletId).info.name;
2022-10-30 17:13:32 +00:00
});
2022-12-05 22:36:28 +00:00
2022-10-30 17:13:32 +00:00
super.initState();
}
@override
void dispose() {
2022-12-05 22:36:28 +00:00
controller.dispose();
focusNode.removeListener(listenerFunc);
2022-10-30 17:13:32 +00:00
super.dispose();
}
@override
Widget build(BuildContext context) {
return TextField(
autocorrect: !Util.isDesktop,
enableSuggestions: !Util.isDesktop,
controller: controller,
focusNode: focusNode,
readOnly: readOnly,
onTap: () {
setState(() {
readOnly = false;
});
},
onEditingComplete: () {
setState(() {
readOnly = true;
});
2022-12-05 22:36:28 +00:00
onDone.call();
2022-10-30 17:13:32 +00:00
},
2022-12-05 22:36:28 +00:00
style: STextStyles.desktopH3(context),
2022-10-30 17:13:32 +00:00
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(
vertical: 4,
horizontal: 12,
),
border: inputBorder,
focusedBorder: inputBorder,
disabledBorder: inputBorder,
enabledBorder: inputBorder,
errorBorder: inputBorder,
fillColor: readOnly
? Colors.transparent
: Theme.of(context).extension<StackColors>()!.textFieldDefaultBG,
),
);
}
}