forked from hnvn/flutter_pattern_formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix issue hnvn#19: Add hour and minute formatter
- Loading branch information
1 parent
ae793e2
commit 1e5d694
Showing
15 changed files
with
288 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
#!/bin/sh | ||
# This is a generated file; do not edit or check into version control. | ||
export "FLUTTER_ROOT=/Users/hahung/Developer/flutter" | ||
export "FLUTTER_APPLICATION_PATH=/Users/hahung/Developer/Workspace/flutter/flutter_pattern_formatter/example" | ||
export "FLUTTER_ROOT=/Users/loritombuta/development/flutter" | ||
export "FLUTTER_APPLICATION_PATH=/Users/loritombuta/Documents/GitHub/flutter_pattern_formatter/example" | ||
export "COCOAPODS_PARALLEL_CODE_SIGN=true" | ||
export "FLUTTER_TARGET=/Users/hahung/Developer/Workspace/flutter/flutter_pattern_formatter/example/lib/main.dart" | ||
export "FLUTTER_TARGET=/Users/loritombuta/Documents/GitHub/flutter_pattern_formatter/example/lib/main.dart" | ||
export "FLUTTER_BUILD_DIR=build" | ||
export "FLUTTER_BUILD_NAME=1.0.0" | ||
export "FLUTTER_BUILD_NUMBER=1" | ||
export "DART_DEFINES=RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==,RkxVVFRFUl9XRUJfQ0FOVkFTS0lUX1VSTD1odHRwczovL3d3dy5nc3RhdGljLmNvbS9mbHV0dGVyLWNhbnZhc2tpdC9iNGZiMTEyMTRkZDJkZGE2Y2UwMTJkZDk4ZWE0OThlOWU4YjkxMjYyLw==" | ||
export "DART_DEFINES=RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==" | ||
export "DART_OBFUSCATION=false" | ||
export "TRACK_WIDGET_CREATION=true" | ||
export "TREE_SHAKE_ICONS=false" | ||
export "PACKAGE_CONFIG=/Users/hahung/Developer/Workspace/flutter/flutter_pattern_formatter/example/.dart_tool/package_config.json" | ||
export "PACKAGE_CONFIG=/Users/loritombuta/Documents/GitHub/flutter_pattern_formatter/example/.dart_tool/package_config.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This is a basic Flutter widget test. | ||
// | ||
// To perform an interaction with a widget in your test, use the WidgetTester | ||
// utility in the flutter_test package. For example, you can send tap and scroll | ||
// gestures. You can also use WidgetTester to find child widgets in the widget | ||
// tree, read text, and verify that the values of widget properties are correct. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'package:example/main.dart'; | ||
|
||
void main() { | ||
testWidgets('Counter increments smoke test', (WidgetTester tester) async { | ||
// Build our app and trigger a frame. | ||
await tester.pumpWidget(MyApp()); | ||
|
||
// Verify that our counter starts at 0. | ||
expect(find.text('0'), findsOneWidget); | ||
expect(find.text('1'), findsNothing); | ||
|
||
// Tap the '+' icon and trigger a frame. | ||
await tester.tap(find.byIcon(Icons.add)); | ||
await tester.pump(); | ||
|
||
// Verify that our counter has incremented. | ||
expect(find.text('0'), findsNothing); | ||
expect(find.text('1'), findsOneWidget); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const INDEX_NOT_FOUND = -1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:pattern_formatter/consts.dart'; | ||
|
||
class HourMinuteInputFormatter extends TextInputFormatter { | ||
String _placeholder = '--:--'; | ||
TextEditingValue? _lastNewValue; | ||
|
||
@override | ||
TextEditingValue formatEditUpdate( | ||
TextEditingValue oldValue, TextEditingValue newValue) { | ||
/// provides placeholder text when user start editing | ||
if (oldValue.text.isEmpty) { | ||
oldValue = oldValue.copyWith( | ||
text: _placeholder, | ||
); | ||
newValue = newValue.copyWith( | ||
text: _fillInputToPlaceholder(newValue.text), | ||
); | ||
return newValue; | ||
} | ||
|
||
/// nothing changes, nothing to do | ||
if (newValue == _lastNewValue) { | ||
return oldValue; | ||
} | ||
_lastNewValue = newValue; | ||
|
||
int offset = newValue.selection.baseOffset; | ||
|
||
/// restrict user's input within the length of date form | ||
if (offset > 5) { | ||
return oldValue; | ||
} | ||
|
||
if (oldValue.text == newValue.text && oldValue.text.length > 0) { | ||
return newValue; | ||
} | ||
|
||
final String oldText = oldValue.text; | ||
final String newText = newValue.text; | ||
String? resultText; | ||
|
||
/// handle user editing, there're two cases: | ||
/// 1. user add new digit: replace '-' at cursor's position by user's input. | ||
/// 2. user delete digit: replace digit at cursor's position by '-' | ||
int index = _indexOfDifference(newText, oldText); | ||
if (oldText.length < newText.length) { | ||
/// add new digit | ||
String newChar = newText[index]; | ||
if (index == 2) { | ||
index++; | ||
offset++; | ||
} | ||
resultText = oldText.replaceRange(index, index + 1, newChar); | ||
if (offset == 2) { | ||
offset++; | ||
} | ||
} else if (oldText.length > newText.length) { | ||
/// delete digit | ||
if (oldText[index] != ':') { | ||
resultText = oldText.replaceRange(index, index + 1, '-'); | ||
if (offset == 3) { | ||
offset--; | ||
} | ||
} else { | ||
resultText = oldText; | ||
} | ||
} | ||
|
||
return oldValue.copyWith( | ||
text: resultText, | ||
selection: TextSelection.collapsed(offset: offset), | ||
composing: defaultTargetPlatform == TargetPlatform.iOS | ||
? TextRange(start: 0, end: 0) | ||
: TextRange.empty, | ||
); | ||
} | ||
|
||
int _indexOfDifference(String? cs1, String? cs2) { | ||
if (cs1 == cs2) { | ||
return INDEX_NOT_FOUND; | ||
} | ||
if (cs1 == null || cs2 == null) { | ||
return 0; | ||
} | ||
int i; | ||
for (i = 0; i < cs1.length && i < cs2.length; ++i) { | ||
if (cs1[i] != cs2[i]) { | ||
break; | ||
} | ||
} | ||
if (i < cs2.length || i < cs1.length) { | ||
return i; | ||
} | ||
return INDEX_NOT_FOUND; | ||
} | ||
|
||
String _fillInputToPlaceholder(String? input) { | ||
if (input == null || input.isEmpty) { | ||
return _placeholder; | ||
} | ||
String result = _placeholder; | ||
final index = [0, 1, 3, 4]; | ||
final length = min(index.length, input.length); | ||
for (int i = 0; i < length; i++) { | ||
result = result.replaceRange(index[i], index[i] + 1, input[i]); | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.