Skip to content

Instantly share code, notes, and snippets.

@roipeker
Last active August 17, 2021 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roipeker/350c096fe455dddabd4b9f1c54d69877 to your computer and use it in GitHub Desktop.
Save roipeker/350c096fe455dddabd4b9f1c54d69877 to your computer and use it in GitHub Desktop.
sample keyboard
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'keyboard test',
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: KeyboardBarInput(
child: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
TextEditingController myController = TextEditingController();
void _incrementCounter() {
setState(() {
_counter++;
});
}
FocusNode focusNode = FocusNode();
TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: Text('You have pushed the button this many times:'),
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
TextButton(
child: Text('open keyboard'),
onPressed: openKeyboard,
),
TextField(
controller: myController,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
String lastTextInput = '';
Future<void> openKeyboard() async {
final control = KeyboardBarInput.of(context);
if (control.isOpen) {
control.close();
return;
}
lastTextInput = await control.open(text: 'Hello');
myController.text = lastTextInput;
// setState(() {});
print("search text: $lastTextInput");
}
}
class KeyboardBarInput extends StatefulWidget {
final Widget child;
const KeyboardBarInput({
Key? key,
required this.child,
}) : super(key: key);
@override
KeyboardBarInputState createState() => KeyboardBarInputState();
static KeyboardBarInputState of(BuildContext context) {
return context.findAncestorStateOfType<KeyboardBarInputState>()!;
}
}
class KeyboardBarInputState extends State<KeyboardBarInput>
with WidgetsBindingObserver {
double keyboardHeight = 0.0;
late final FocusNode _focusNode = FocusNode();
late final TextEditingController _textController = TextEditingController();
bool get isOpen => keyboardHeight > 0;
late Completer<String> _completer;
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
@override
void didChangeMetrics() {
// keyboardHeight = WidgetsBinding.instance?.window.viewInsets.bottom ?? 0.0;
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
setState(() {
if (_focusNode.hasFocus) {
final mq = MediaQuery.of(context);
keyboardHeight = mq.viewInsets.bottom;
} else {
keyboardHeight = 0;
}
});
});
}
@override
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
_textController.dispose();
_focusNode.dispose();
super.dispose();
}
void _onCancelTap() {
_completer.complete(Future.value(''));
close();
}
void _onDoneTap() {
_completer.complete(Future.value(_textController.text));
close();
}
@override
Widget build(BuildContext context) {
const baseColor = CupertinoDynamicColor.withBrightness(
color: Color(0xf3D6D7DC),
darkColor: Color(0xf3373737),
);
final textColor = Theme.of(context).textTheme.bodyText1!.color;
return Stack(
children: [
Positioned.fill(child: widget.child),
Positioned(
bottom: keyboardHeight,
left: 0,
right: 0,
child: Visibility(
maintainState: true,
maintainAnimation: true,
maintainInteractivity: true,
maintainSize: true,
visible: isOpen,
child: Container(
color: baseColor.resolveFrom(context),
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
child: Row(
children: [
Expanded(
child: CupertinoTextField(
focusNode: _focusNode,
controller: _textController,
enableInteractiveSelection: false,
keyboardType: TextInputType.name,
autocorrect: false,
style: TextStyle(color: textColor),
enableSuggestions: false,
expands: false,
maxLines: 1,
onSubmitted: (text) {
_completer.complete(Future.value(text));
},
),
),
CupertinoButton(child: Text('Done'), onPressed: _onDoneTap),
CupertinoButton(
child: Text('Cancel'), onPressed: _onCancelTap),
],
),
),
),
),
],
);
}
void close() {
_focusNode.unfocus();
}
Future<String> open({String? text}) async {
if (text == null) {
_textController.clear();
} else {
_textController.text = text;
}
_focusNode.requestFocus();
_completer = Completer<String>();
return _completer.future;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment