[infra_ui][keyboard] Add keyboard demo entrance

This commit is contained in:
Jaylen Bian 2021-07-17 00:08:54 +08:00
parent 4364f59e39
commit be4f6a8250
3 changed files with 62 additions and 3 deletions

View File

@ -5,7 +5,7 @@ abstract class ListItem {}
abstract class DemoItem extends ListItem { abstract class DemoItem extends ListItem {
String buildTitle(); String buildTitle();
void handleTap(); void handleTap(BuildContext context);
} }
class SectionHeaderItem extends ListItem { class SectionHeaderItem extends ListItem {

View File

@ -1,11 +1,13 @@
import 'package:example/home/demo_item.dart'; import 'package:example/keyboard/keyboard_screen.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'demo_item.dart';
class HomeScreen extends StatelessWidget { class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key); const HomeScreen({Key? key}) : super(key: key);
static List<ListItem> items = [ static List<ListItem> items = [
SectionHeaderItem('Widget Demos'), SectionHeaderItem('Widget Demos'),
KeyboardItem(),
]; ];
@override @override
@ -30,7 +32,7 @@ class HomeScreen extends StatelessWidget {
} else if (item is DemoItem) { } else if (item is DemoItem) {
return ListTile( return ListTile(
title: Text(item.buildTitle()), title: Text(item.buildTitle()),
onTap: item.handleTap, onTap: () => item.handleTap(context),
); );
} }
return const ListTile( return const ListTile(

View File

@ -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<KeyboardScreen> {
@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();
}
}
}