stack_wallet/lib/widgets/hover_text_field.dart

146 lines
3.9 KiB
Dart
Raw Normal View History

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 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/providers/global/wallets_service_provider.dart';
import 'package:stackwallet/themes/stack_colors.dart';
2022-10-30 17:13:32 +00:00
import 'package:stackwallet/utilities/constants.dart';
2022-12-05 22:36:28 +00:00
import 'package:stackwallet/utilities/text_styles.dart';
2022-10-30 17:13:32 +00:00
import 'package:stackwallet/utilities/util.dart';
2022-12-05 22:36:28 +00:00
class DesktopWalletNameField extends ConsumerStatefulWidget {
const DesktopWalletNameField({
2022-10-30 17:13:32 +00:00
Key? key,
2022-12-05 22:36:28 +00:00
required this.walletId,
2022-10-30 17:13:32 +00:00
}) : super(key: key);
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 currentWalletName = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.walletName;
final newName = controller.text;
if (newName != currentWalletName) {
final success =
await ref.read(walletsServiceChangeNotifierProvider).renameWallet(
from: currentWalletName,
to: newName,
shouldNotifyListeners: true,
);
if (success) {
ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.walletName = newName;
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(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.walletName;
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,
),
);
}
}