diff --git a/app_flowy/packages/flowy_infra_ui/example/lib/home/demo_item.dart b/app_flowy/packages/flowy_infra_ui/example/lib/home/demo_item.dart index da6051f1c7..361e234231 100644 --- a/app_flowy/packages/flowy_infra_ui/example/lib/home/demo_item.dart +++ b/app_flowy/packages/flowy_infra_ui/example/lib/home/demo_item.dart @@ -5,7 +5,7 @@ abstract class ListItem {} abstract class DemoItem extends ListItem { String buildTitle(); - void handleTap(); + void handleTap(BuildContext context); } class SectionHeaderItem extends ListItem { diff --git a/app_flowy/packages/flowy_infra_ui/example/lib/home/home_screen.dart b/app_flowy/packages/flowy_infra_ui/example/lib/home/home_screen.dart index 7f0ca3fb6b..ca9e17d8c1 100644 --- a/app_flowy/packages/flowy_infra_ui/example/lib/home/home_screen.dart +++ b/app_flowy/packages/flowy_infra_ui/example/lib/home/home_screen.dart @@ -1,11 +1,13 @@ -import 'package:example/home/demo_item.dart'; +import 'package:example/keyboard/keyboard_screen.dart'; import 'package:flutter/material.dart'; +import 'demo_item.dart'; class HomeScreen extends StatelessWidget { const HomeScreen({Key? key}) : super(key: key); static List items = [ SectionHeaderItem('Widget Demos'), + KeyboardItem(), ]; @override @@ -30,7 +32,7 @@ class HomeScreen extends StatelessWidget { } else if (item is DemoItem) { return ListTile( title: Text(item.buildTitle()), - onTap: item.handleTap, + onTap: () => item.handleTap(context), ); } return const ListTile( diff --git a/app_flowy/packages/flowy_infra_ui/example/lib/keyboard/keyboard_screen.dart b/app_flowy/packages/flowy_infra_ui/example/lib/keyboard/keyboard_screen.dart new file mode 100644 index 0000000000..c459f61668 --- /dev/null +++ b/app_flowy/packages/flowy_infra_ui/example/lib/keyboard/keyboard_screen.dart @@ -0,0 +1,57 @@ +import 'package:flutter/material.dart'; +import '../home/demo_item.dart'; + +class KeyboardItem extends DemoItem { + @override + String buildTitle() => 'Keyboard Listener'; + + @override + void handleTap(BuildContext context) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) { + return const KeyboardScreen(); + }, + ), + ); + } +} + +class KeyboardScreen extends StatefulWidget { + const KeyboardScreen({Key? key}) : super(key: key); + + @override + _KeyboardScreenState createState() => _KeyboardScreenState(); +} + +class _KeyboardScreenState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Keyboard Visibility Demo'), + ), + body: GestureDetector( + onTap: () => _dismissKeyboard(context), + behavior: HitTestBehavior.translucent, + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 36), + child: Center( + child: TextField( + style: const TextStyle(fontSize: 20), + controller: TextEditingController(text: 'Test'), + ), + ), + ), + ), + ); + } + + void _dismissKeyboard(BuildContext context) { + final currentFocus = FocusScope.of(context); + + if (!currentFocus.hasPrimaryFocus && currentFocus.hasFocus) { + FocusManager.instance.primaryFocus?.unfocus(); + } + } +}