|
| 1 | +// Copyright 2026 Samsung Electronics Co., Ltd. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// ignore_for_file: public_member_api_docs |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:keyboard_detection_tizen/keyboard_detection_tizen.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + runApp(const MyApp()); |
| 12 | +} |
| 13 | + |
| 14 | +class MyApp extends StatelessWidget { |
| 15 | + const MyApp({super.key}); |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + return MaterialApp( |
| 20 | + title: 'keyboard_detection_tizen example', |
| 21 | + theme: ThemeData(primarySwatch: Colors.blue), |
| 22 | + home: const HomePage(), |
| 23 | + ); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class HomePage extends StatefulWidget { |
| 28 | + const HomePage({super.key}); |
| 29 | + |
| 30 | + @override |
| 31 | + State<HomePage> createState() => _HomePageState(); |
| 32 | +} |
| 33 | + |
| 34 | +class _HomePageState extends State<HomePage> { |
| 35 | + late final KeyboardDetectionController _controller; |
| 36 | + |
| 37 | + @override |
| 38 | + void initState() { |
| 39 | + super.initState(); |
| 40 | + _controller = KeyboardDetectionController( |
| 41 | + onChanged: (KeyboardState state) { |
| 42 | + debugPrint('keyboard_detection_tizen: $state'); |
| 43 | + }, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + void dispose() { |
| 49 | + _controller.dispose(); |
| 50 | + super.dispose(); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + Widget build(BuildContext context) { |
| 55 | + return Scaffold( |
| 56 | + appBar: AppBar(title: const Text('Keyboard Detection')), |
| 57 | + body: SafeArea( |
| 58 | + child: Padding( |
| 59 | + padding: const EdgeInsets.all(24), |
| 60 | + child: Column( |
| 61 | + mainAxisAlignment: MainAxisAlignment.center, |
| 62 | + children: <Widget>[ |
| 63 | + StreamBuilder<KeyboardState>( |
| 64 | + stream: _controller.stream, |
| 65 | + initialData: _controller.state, |
| 66 | + builder: (BuildContext _, AsyncSnapshot<KeyboardState> snap) { |
| 67 | + final KeyboardState s = snap.data ?? KeyboardState.unknown; |
| 68 | + return Column( |
| 69 | + key: const Key('status'), |
| 70 | + children: <Widget>[ |
| 71 | + Text( |
| 72 | + 'state: ${s.name}', |
| 73 | + key: const Key('state-text'), |
| 74 | + style: Theme.of(context).textTheme.titleMedium, |
| 75 | + ), |
| 76 | + const SizedBox(height: 8), |
| 77 | + Text( |
| 78 | + 'visible: ${_controller.stateAsBool() ?? "unknown"}', |
| 79 | + key: const Key('visible-text'), |
| 80 | + ), |
| 81 | + const SizedBox(height: 8), |
| 82 | + Text( |
| 83 | + 'width: ${_controller.width.toStringAsFixed(1)} ' |
| 84 | + '/ size(height): ${_controller.size.toStringAsFixed(1)} ', |
| 85 | + key: const Key('size-text'), |
| 86 | + ), |
| 87 | + const SizedBox(height: 8), |
| 88 | + Text( |
| 89 | + 'position: ' |
| 90 | + '(${_controller.position.dx.toStringAsFixed(1)}, ' |
| 91 | + '${_controller.position.dy.toStringAsFixed(1)})', |
| 92 | + key: const Key('position-text'), |
| 93 | + ), |
| 94 | + ], |
| 95 | + ); |
| 96 | + }, |
| 97 | + ), |
| 98 | + const SizedBox(height: 32), |
| 99 | + const TextField( |
| 100 | + decoration: InputDecoration( |
| 101 | + labelText: 'Tap here to open keyboard', |
| 102 | + ), |
| 103 | + ), |
| 104 | + ], |
| 105 | + ), |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments