| 1 | import 'dart:io'; |
| 2 | |
| 3 | import 'package:cake_wallet/src/widgets/cake_image_widget.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:flutter_svg/flutter_svg.dart'; |
| 6 | |
| 7 | class KeyboardHideOverlay extends StatelessWidget { |
| 8 | const KeyboardHideOverlay({super.key, required this.child, this.unfocusOnTap = false}); |
| 9 | |
| 10 | final Widget child; |
| 11 | final bool unfocusOnTap; |
| 12 | |
| 13 | @override |
| 14 | Widget build(BuildContext context) { |
| 15 | return Stack( |
| 16 | children: [ |
| 17 | Positioned.fill( |
| 18 | child: GestureDetector( |
| 19 | onTap: () { |
| 20 | if (unfocusOnTap) { |
| 21 | FocusManager.instance.primaryFocus?.unfocus(); |
| 22 | } |
| 23 | }, |
| 24 | child: child)), |
| 25 | if (Platform.isIOS) KeyboardHideButton() |
| 26 | ], |
| 27 | ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | class KeyboardHideButton extends StatefulWidget { |
| 32 | const KeyboardHideButton({super.key}); |
| 33 | |
| 34 | @override |
| 35 | State<KeyboardHideButton> createState() => _KeyboardHideButtonState(); |
| 36 | } |
| 37 | |
| 38 | class _KeyboardHideButtonState extends State<KeyboardHideButton> { |
| 39 | double _lastInset = 0; |
| 40 | bool _visible = false; |
| 41 | |
| 42 | @override |
| 43 | Widget build(BuildContext context) { |
| 44 | final inset = MediaQuery.viewInsetsOf(context).bottom; |
| 45 | |
| 46 | if (inset != _lastInset) { |
| 47 | _lastInset = inset; |
| 48 | _visible = false; |
| 49 | |
| 50 | if (inset > 0) { |
| 51 | WidgetsBinding.instance.addPostFrameCallback((_) { |
| 52 | if (mounted && MediaQuery.viewInsetsOf(context).bottom == inset) { |
| 53 | setState(() => _visible = true); |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return Align( |
| 60 | alignment: Alignment.bottomRight, |
| 61 | child: Padding( |
| 62 | padding: EdgeInsets.only(bottom: inset), |
| 63 | child: Padding( |
| 64 | padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 18), |
| 65 | child: AnimatedOpacity( |
| 66 | opacity: _visible ? 1 : 0, |
| 67 | duration: const Duration(milliseconds: 100), |
| 68 | child: GestureDetector( |
| 69 | onTap: () => FocusScope.of(context).unfocus(), |
| 70 | child: Container( |
| 71 | decoration: BoxDecoration( |
| 72 | color: Theme.of(context).colorScheme.primary, |
| 73 | borderRadius: BorderRadius.circular(999), |
| 74 | ), |
| 75 | child: Padding( |
| 76 | padding: const EdgeInsets.all(8.0), |
| 77 | child: CakeImageWidget( |
| 78 | imageUrl: "assets/new-ui/hide_keyboard.svg", |
| 79 | colorFilter: ColorFilter.mode( |
| 80 | Theme.of(context).colorScheme.onPrimary, BlendMode.srcIn), |
| 81 | ), |
| 82 | )), |
| 83 | ), |
| 84 | ), |
| 85 | ), |
| 86 | ), |
| 87 | ); |
| 88 | } |
| 89 | } |