From be4f6a8250984ee79dd9486320b50471ad594c5f Mon Sep 17 00:00:00 2001 From: Jaylen Bian Date: Sat, 17 Jul 2021 00:08:54 +0800 Subject: [PATCH] [infra_ui][keyboard] Add keyboard demo entrance --- .../example/lib/home/demo_item.dart | 2 +- .../example/lib/home/home_screen.dart | 6 +- .../example/lib/keyboard/keyboard_screen.dart | 57 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 app_flowy/packages/flowy_infra_ui/example/lib/keyboard/keyboard_screen.dart 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(); + } + } +}