Skip to content

Commit

Permalink
Merge pull request #1 from tashigeekyants/master
Browse files Browse the repository at this point in the history
added optional onErrorCallback to handle custom error
  • Loading branch information
CodingWithTashi authored Dec 2, 2022
2 parents 33916ad + f01d5dc commit c408db0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 49 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 0.0.1

* Initial Release
* load text from assets/file
* load text from assets/file
* search text by keyword

## 0.0.2
Expand All @@ -14,8 +14,12 @@

## 0.0.4
* textStyle issue fixed
* default focus color change to amber
* default focus color change to amber
* refactor code

## 0.0.5
* Updated readme md and github action

## 0.0.6
* documentation update
* added onErrorCallback for handle error
7 changes: 7 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_text_viewer/flutter_text_viewer.dart';

Expand Down Expand Up @@ -45,6 +46,12 @@ class _HomePageState extends State<HomePage> {
highLightColor: Colors.yellow,
focusColor: Colors.orange,
ignoreCase: true,
onErrorCallback: (error) {
// show error in your UI
if (kDebugMode) {
print("Error: $error");
}
},
),
showSearchAppBar: true,
);
Expand Down
33 changes: 13 additions & 20 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,21 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
Expand All @@ -56,7 +49,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -80,7 +73,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.4"
version: "0.0.6"
image_painter:
dependency: "direct main"
description:
Expand All @@ -101,28 +94,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -134,7 +127,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -155,21 +148,21 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
version: "0.4.12"
vector_math:
dependency: transitive
description:
Expand Down
50 changes: 30 additions & 20 deletions lib/model/text_viewer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';

typedef OnErrorCallback = void Function(String error);

class TextViewer {
/// Provide the path of the file that need to load
/// In this case you need to pass .txt file
Expand Down Expand Up @@ -36,10 +38,14 @@ class TextViewer {
///Search text case is sensitive or not
final bool ignoreCase;

///Search on error Callback
final OnErrorCallback? onErrorCallback;

const TextViewer._({
this.assetPath,
this.filePath,
this.textContent,
this.onErrorCallback,
required this.textStyle,
required this.focusTextStyle,
required this.highLightTextStyle,
Expand All @@ -56,18 +62,19 @@ class TextViewer {
Color highLightColor = Colors.yellow,
Color focusColor = Colors.amber,
bool ignoreCase = true,
OnErrorCallback? onErrorCallback,
}) {
return TextViewer._(
assetPath: assetPath,
textStyle: textStyle,
highLightTextStyle: highLightTextStyle ??
textStyle.copyWith(background: Paint()..color = highLightColor),
focusTextStyle: focusTextStyle ??
textStyle.copyWith(background: Paint()..color = focusColor),
highLightColor: highLightColor,
focusColor: focusColor,
ignoreCase: ignoreCase,
);
assetPath: assetPath,
textStyle: textStyle,
highLightTextStyle: highLightTextStyle ??
textStyle.copyWith(background: Paint()..color = highLightColor),
focusTextStyle: focusTextStyle ??
textStyle.copyWith(background: Paint()..color = focusColor),
highLightColor: highLightColor,
focusColor: focusColor,
ignoreCase: ignoreCase,
onErrorCallback: onErrorCallback);
}

factory TextViewer.file(
Expand All @@ -78,6 +85,7 @@ class TextViewer {
Color highLightColor = Colors.yellow,
Color focusColor = Colors.amber,
bool ignoreCase = true,
OnErrorCallback? onErrorCallback,
}) {
return TextViewer._(
filePath: filePath,
Expand All @@ -89,6 +97,7 @@ class TextViewer {
highLightColor: highLightColor,
focusColor: focusColor,
ignoreCase: ignoreCase,
onErrorCallback: onErrorCallback,
);
}

Expand All @@ -100,17 +109,18 @@ class TextViewer {
Color highLightColor = Colors.yellow,
Color focusColor = Colors.amber,
bool ignoreCase = true,
OnErrorCallback? onErrorCallback,
}) {
return TextViewer._(
textContent: textContent,
textStyle: textStyle,
highLightTextStyle: highLightTextStyle ??
textStyle.copyWith(background: Paint()..color = highLightColor),
focusTextStyle: focusTextStyle ??
textStyle.copyWith(background: Paint()..color = focusColor),
highLightColor: highLightColor,
focusColor: focusColor,
ignoreCase: ignoreCase,
);
textContent: textContent,
textStyle: textStyle,
highLightTextStyle: highLightTextStyle ??
textStyle.copyWith(background: Paint()..color = highLightColor),
focusTextStyle: focusTextStyle ??
textStyle.copyWith(background: Paint()..color = focusColor),
highLightColor: highLightColor,
focusColor: focusColor,
ignoreCase: ignoreCase,
onErrorCallback: onErrorCallback);
}
}
11 changes: 8 additions & 3 deletions lib/screen/text_viewer_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io' if (dart.library.html) 'package:flutter_text_viewer/web.dart';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_text_viewer/model/text_viewer.dart';
Expand Down Expand Up @@ -126,9 +127,13 @@ class _TextViewerPageState extends State<TextViewerPage> {
searchValue = value;
});
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Enter some value to search'),
));
if (widget.textViewer.onErrorCallback != null) {
widget.textViewer.onErrorCallback!('Enter some value to search');
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Enter some value to search'),
));
}
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions lib/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class File {
File(this.path) {
throw PlatformException(
code: 'Platform Exception',
message: 'ImageViewer.file does not support in web',
message: 'TextViewer.file does not support in web',
);
}
String readAsStringSync() {
throw PlatformException(
code: 'Platform Exception',
message: 'ImageViewer.file does not support in web');
message: 'TextViewer.file does not support in web');
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_text_viewer
description: Flutter textviewer package provide easy and simple way to load and search text from assets,file and even direct input string.
version: 0.0.5
version: 0.0.6
homepage: https://github.com/CodingWithTashi/flutter_text_viewer
issue_tracker: https://github.com/CodingWithTashi/flutter_text_viewer/issues

Expand All @@ -15,6 +15,6 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter_lints: ^2.0.1

flutter:

0 comments on commit c408db0

Please sign in to comment.