Skip to content

Commit

Permalink
Builder docs (MisterJimson#43)
Browse files Browse the repository at this point in the history
* Add KeyboardVisibilityBuilder

This is an alternative method of handling Keyboard Visibility subscription that is pretty painless to use and matches many of the patterns found within the core Flutter library

* use KeyboardVisibility.isVisible instead of false

* fix imports pasted by mistake

* update samples

* docs + version

Co-authored-by: Luke Pighetti <[email protected]>
  • Loading branch information
MisterJimson and lukepighetti authored Nov 6, 2020
1 parent 2064941 commit 1ed4257
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 128 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [3.3.0] - November 6, 2020

Thanks to lukepighetti for this feature

* Added `KeyboardVisibilityBuilder` to access keyboard visibility with the builder pattern

## [3.2.2] - August 26, 2020

* MissingPluginException if no longer thrown during `flutter test` if you first call `KeyboardVisibility.setVisibilityForTesting(value)` in your test
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ This is a fork, original project [here](https://github.com/adee42/flutter_keyboa

## Install
Add the dependency to your pubspec.yaml
```
```yaml
dependencies:
flutter_keyboard_visibility: ^3.2.2
flutter_keyboard_visibility: ^3.3.0
```
## Usage: React to Keyboard Visibility Changes
### Option 1: Within your `Widget` tree using a builder
Build your Widget tree based on whether or not the keyboard is visible by using `KeyboardVisibilityBuilder`.
```dart
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
### Option 1: Within your `Widget` tree
/// In any of your widgets...
@override
Widget build(BuildContext context) {
return KeyboardVisibilityBuilder(
builder: (context, isKeyboardVisible) {
return Text(
'The keyboard is: ${isKeyboardVisible ? 'VISIBLE' : 'NOT VISIBLE'}',
);
}
);
```
### Option 2: Within your `Widget` tree using a provider
Build your `Widget` tree based on whether or not the keyboard is
visible by including a `KeyboardVisibilityProvider` near the top
of your tree.
Expand All @@ -38,7 +53,7 @@ Widget build(BuildContext context) {
}
```

### Option 2: Direct query and subscription
### Option 3: Direct query and subscription

Query and/or subscribe to keyboard visibility directly with the
`KeyboardVisibility` class.
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
Expand Down
74 changes: 14 additions & 60 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,29 @@ project 'Runner', {
'Release' => :release,
}

def parse_KV_file(file, separator='=')
file_abs_path = File.expand_path(file)
if !File.exists? file_abs_path
return [];
def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
generated_key_values = {}
skip_line_start_symbols = ["#", "/"]
File.foreach(file_abs_path) do |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
generated_key_values[podname] = podpath
else
puts "Invalid plugin specification: #{line}"
end
end
generated_key_values
end

target 'Runner' do
# Flutter Pod

copied_flutter_dir = File.join(__dir__, 'Flutter')
copied_framework_path = File.join(copied_flutter_dir, 'Flutter.framework')
copied_podspec_path = File.join(copied_flutter_dir, 'Flutter.podspec')
unless File.exist?(copied_framework_path) && File.exist?(copied_podspec_path)
# Copy Flutter.framework and Flutter.podspec to Flutter/ to have something to link against if the xcode backend script has not run yet.
# That script will copy the correct debug/profile/release version of the framework based on the currently selected Xcode configuration.
# CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.

generated_xcode_build_settings_path = File.join(copied_flutter_dir, 'Generated.xcconfig')
unless File.exist?(generated_xcode_build_settings_path)
raise "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
generated_xcode_build_settings = parse_KV_file(generated_xcode_build_settings_path)
cached_framework_dir = generated_xcode_build_settings['FLUTTER_FRAMEWORK_DIR'];

unless File.exist?(copied_framework_path)
FileUtils.cp_r(File.join(cached_framework_dir, 'Flutter.framework'), copied_flutter_dir)
end
unless File.exist?(copied_podspec_path)
FileUtils.cp(File.join(cached_framework_dir, 'Flutter.podspec'), copied_flutter_dir)
end
File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

# Keep pod path relative so it can be checked into Podfile.lock.
pod 'Flutter', :path => 'Flutter'
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

# Plugin Pods
flutter_ios_podfile_setup

# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
# referring to absolute paths on developers' machines.
system('rm -rf .symlinks')
system('mkdir -p .symlinks/plugins')
plugin_pods = parse_KV_file('../.flutter-plugins')
plugin_pods.each do |name, path|
symlink = File.join('.symlinks', 'plugins', name)
File.symlink(path, symlink)
pod name, :path => File.join(symlink, 'ios')
end
target 'Runner' do
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
flutter_additional_ios_build_settings(target)
end
end
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_keyboard_visibility: 0339d06371254c3eb25eeb90ba8d17dca8f9c069

PODFILE CHECKSUM: f32fb4e7c14f8b3ca19a369d7be425dd9241af27
PODFILE CHECKSUM: 8e679eca47255a8ca8067c4c67aab20e64cb974d

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
9 changes: 4 additions & 5 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,12 @@
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../Flutter/Flutter.framework",
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down
30 changes: 6 additions & 24 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,7 @@ void main() {
runApp(MyApp());
}

class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
bool _keyboardState;

@override
void initState() {
super.initState();
_keyboardState = KeyboardVisibility.isVisible;
KeyboardVisibility.onChange.listen((bool visible) {
setState(() {
_keyboardState = visible;
});
});
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -47,9 +27,11 @@ class _MyAppState extends State<MyApp> {
),
),
Container(height: 60.0),
Text(
'The keyboard is: ${_keyboardState ? 'VISIBLE' : 'NOT VISIBLE'}',
),
KeyboardVisibilityBuilder(builder: (context, visible) {
return Text(
'The keyboard is: ${visible ? 'VISIBLE' : 'NOT VISIBLE'}',
);
}),
],
),
),
Expand Down
1 change: 1 addition & 0 deletions example_old/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.packages
Expand Down
2 changes: 1 addition & 1 deletion example_old/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: c34e2287a9ccaa606aeceab922830efb9a6ff69a

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
3 changes: 0 additions & 3 deletions example_old/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@
/* Begin XCBuildConfiguration section */
249021D3217E4FDB00AE95B9 /* Profile */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -390,7 +389,6 @@
};
97C147031CF9000F007C117D /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -446,7 +444,6 @@
};
97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down
36 changes: 9 additions & 27 deletions example_old/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,12 @@ void main() {
runApp(MyApp());
}

class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
bool _keyboardState;

@override
void initState() {
super.initState();
_keyboardState = KeyboardVisibility.isVisible;
KeyboardVisibility.onChange.listen((bool visible) {
setState(() {
_keyboardState = visible;
});
});
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return KeyboardDismissOnTap(
child: MaterialApp(
home: Scaffold(
return MaterialApp(
home: KeyboardDismissOnTap(
child: Scaffold(
appBar: AppBar(
title: Text('Keyboard Visibility Example'),
),
Expand All @@ -47,9 +27,11 @@ class _MyAppState extends State<MyApp> {
),
),
Container(height: 60.0),
Text(
'The keyboard is: ${_keyboardState ? 'VISIBLE' : 'NOT VISIBLE'}',
),
KeyboardVisibilityBuilder(builder: (context, visible) {
return Text(
'The keyboard is: ${visible ? 'VISIBLE' : 'NOT VISIBLE'}',
);
}),
],
),
),
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: flutter_keyboard_visibility
description: Flutter plugin for discovering the state of the soft-keyboard visibility on Android and iOS.
version: 3.2.2
version: 3.3.0
homepage: https://github.com/MisterJimson/flutter_keyboard_visibility

environment:
sdk: ">=2.1.0 <3.0.0"
flutter: ">=1.12.13+hotfix.6 <2.0.0"
flutter: ">=1.12.13+hotfix.6"

dependencies:
flutter:
Expand Down

0 comments on commit 1ed4257

Please sign in to comment.