Skip to content

Commit c408db0

Browse files
Merge pull request #1 from tashigeekyants/master
added optional onErrorCallback to handle custom error
2 parents 33916ad + f01d5dc commit c408db0

File tree

7 files changed

+68
-49
lines changed

7 files changed

+68
-49
lines changed

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## 0.0.1
22

33
* Initial Release
4-
* load text from assets/file
4+
* load text from assets/file
55
* search text by keyword
66

77
## 0.0.2
@@ -14,8 +14,12 @@
1414

1515
## 0.0.4
1616
* textStyle issue fixed
17-
* default focus color change to amber
17+
* default focus color change to amber
1818
* refactor code
1919

2020
## 0.0.5
2121
* Updated readme md and github action
22+
23+
## 0.0.6
24+
* documentation update
25+
* added onErrorCallback for handle error

example/lib/main.dart

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_text_viewer/flutter_text_viewer.dart';
34

@@ -45,6 +46,12 @@ class _HomePageState extends State<HomePage> {
4546
highLightColor: Colors.yellow,
4647
focusColor: Colors.orange,
4748
ignoreCase: true,
49+
onErrorCallback: (error) {
50+
// show error in your UI
51+
if (kDebugMode) {
52+
print("Error: $error");
53+
}
54+
},
4855
),
4956
showSearchAppBar: true,
5057
);

example/pubspec.lock

+13-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.8.2"
10+
version: "2.9.0"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -21,21 +21,14 @@ packages:
2121
name: characters
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "1.2.0"
25-
charcode:
26-
dependency: transitive
27-
description:
28-
name: charcode
29-
url: "https://pub.dartlang.org"
30-
source: hosted
31-
version: "1.3.1"
24+
version: "1.2.1"
3225
clock:
3326
dependency: transitive
3427
description:
3528
name: clock
3629
url: "https://pub.dartlang.org"
3730
source: hosted
38-
version: "1.1.0"
31+
version: "1.1.1"
3932
collection:
4033
dependency: transitive
4134
description:
@@ -56,7 +49,7 @@ packages:
5649
name: fake_async
5750
url: "https://pub.dartlang.org"
5851
source: hosted
59-
version: "1.3.0"
52+
version: "1.3.1"
6053
flutter:
6154
dependency: "direct main"
6255
description: flutter
@@ -80,7 +73,7 @@ packages:
8073
path: ".."
8174
relative: true
8275
source: path
83-
version: "0.0.4"
76+
version: "0.0.6"
8477
image_painter:
8578
dependency: "direct main"
8679
description:
@@ -101,28 +94,28 @@ packages:
10194
name: matcher
10295
url: "https://pub.dartlang.org"
10396
source: hosted
104-
version: "0.12.11"
97+
version: "0.12.12"
10598
material_color_utilities:
10699
dependency: transitive
107100
description:
108101
name: material_color_utilities
109102
url: "https://pub.dartlang.org"
110103
source: hosted
111-
version: "0.1.4"
104+
version: "0.1.5"
112105
meta:
113106
dependency: transitive
114107
description:
115108
name: meta
116109
url: "https://pub.dartlang.org"
117110
source: hosted
118-
version: "1.7.0"
111+
version: "1.8.0"
119112
path:
120113
dependency: transitive
121114
description:
122115
name: path
123116
url: "https://pub.dartlang.org"
124117
source: hosted
125-
version: "1.8.1"
118+
version: "1.8.2"
126119
sky_engine:
127120
dependency: transitive
128121
description: flutter
@@ -134,7 +127,7 @@ packages:
134127
name: source_span
135128
url: "https://pub.dartlang.org"
136129
source: hosted
137-
version: "1.8.2"
130+
version: "1.9.0"
138131
stack_trace:
139132
dependency: transitive
140133
description:
@@ -155,21 +148,21 @@ packages:
155148
name: string_scanner
156149
url: "https://pub.dartlang.org"
157150
source: hosted
158-
version: "1.1.0"
151+
version: "1.1.1"
159152
term_glyph:
160153
dependency: transitive
161154
description:
162155
name: term_glyph
163156
url: "https://pub.dartlang.org"
164157
source: hosted
165-
version: "1.2.0"
158+
version: "1.2.1"
166159
test_api:
167160
dependency: transitive
168161
description:
169162
name: test_api
170163
url: "https://pub.dartlang.org"
171164
source: hosted
172-
version: "0.4.9"
165+
version: "0.4.12"
173166
vector_math:
174167
dependency: transitive
175168
description:

lib/model/text_viewer.dart

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:flutter/material.dart';
22

3+
typedef OnErrorCallback = void Function(String error);
4+
35
class TextViewer {
46
/// Provide the path of the file that need to load
57
/// In this case you need to pass .txt file
@@ -36,10 +38,14 @@ class TextViewer {
3638
///Search text case is sensitive or not
3739
final bool ignoreCase;
3840

41+
///Search on error Callback
42+
final OnErrorCallback? onErrorCallback;
43+
3944
const TextViewer._({
4045
this.assetPath,
4146
this.filePath,
4247
this.textContent,
48+
this.onErrorCallback,
4349
required this.textStyle,
4450
required this.focusTextStyle,
4551
required this.highLightTextStyle,
@@ -56,18 +62,19 @@ class TextViewer {
5662
Color highLightColor = Colors.yellow,
5763
Color focusColor = Colors.amber,
5864
bool ignoreCase = true,
65+
OnErrorCallback? onErrorCallback,
5966
}) {
6067
return TextViewer._(
61-
assetPath: assetPath,
62-
textStyle: textStyle,
63-
highLightTextStyle: highLightTextStyle ??
64-
textStyle.copyWith(background: Paint()..color = highLightColor),
65-
focusTextStyle: focusTextStyle ??
66-
textStyle.copyWith(background: Paint()..color = focusColor),
67-
highLightColor: highLightColor,
68-
focusColor: focusColor,
69-
ignoreCase: ignoreCase,
70-
);
68+
assetPath: assetPath,
69+
textStyle: textStyle,
70+
highLightTextStyle: highLightTextStyle ??
71+
textStyle.copyWith(background: Paint()..color = highLightColor),
72+
focusTextStyle: focusTextStyle ??
73+
textStyle.copyWith(background: Paint()..color = focusColor),
74+
highLightColor: highLightColor,
75+
focusColor: focusColor,
76+
ignoreCase: ignoreCase,
77+
onErrorCallback: onErrorCallback);
7178
}
7279

7380
factory TextViewer.file(
@@ -78,6 +85,7 @@ class TextViewer {
7885
Color highLightColor = Colors.yellow,
7986
Color focusColor = Colors.amber,
8087
bool ignoreCase = true,
88+
OnErrorCallback? onErrorCallback,
8189
}) {
8290
return TextViewer._(
8391
filePath: filePath,
@@ -89,6 +97,7 @@ class TextViewer {
8997
highLightColor: highLightColor,
9098
focusColor: focusColor,
9199
ignoreCase: ignoreCase,
100+
onErrorCallback: onErrorCallback,
92101
);
93102
}
94103

@@ -100,17 +109,18 @@ class TextViewer {
100109
Color highLightColor = Colors.yellow,
101110
Color focusColor = Colors.amber,
102111
bool ignoreCase = true,
112+
OnErrorCallback? onErrorCallback,
103113
}) {
104114
return TextViewer._(
105-
textContent: textContent,
106-
textStyle: textStyle,
107-
highLightTextStyle: highLightTextStyle ??
108-
textStyle.copyWith(background: Paint()..color = highLightColor),
109-
focusTextStyle: focusTextStyle ??
110-
textStyle.copyWith(background: Paint()..color = focusColor),
111-
highLightColor: highLightColor,
112-
focusColor: focusColor,
113-
ignoreCase: ignoreCase,
114-
);
115+
textContent: textContent,
116+
textStyle: textStyle,
117+
highLightTextStyle: highLightTextStyle ??
118+
textStyle.copyWith(background: Paint()..color = highLightColor),
119+
focusTextStyle: focusTextStyle ??
120+
textStyle.copyWith(background: Paint()..color = focusColor),
121+
highLightColor: highLightColor,
122+
focusColor: focusColor,
123+
ignoreCase: ignoreCase,
124+
onErrorCallback: onErrorCallback);
115125
}
116126
}

lib/screen/text_viewer_page.dart

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:io' if (dart.library.html) 'package:flutter_text_viewer/web.dart';
2+
23
import 'package:flutter/material.dart';
34
import 'package:flutter/services.dart';
45
import 'package:flutter_text_viewer/model/text_viewer.dart';
@@ -126,9 +127,13 @@ class _TextViewerPageState extends State<TextViewerPage> {
126127
searchValue = value;
127128
});
128129
} else {
129-
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
130-
content: Text('Enter some value to search'),
131-
));
130+
if (widget.textViewer.onErrorCallback != null) {
131+
widget.textViewer.onErrorCallback!('Enter some value to search');
132+
} else {
133+
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
134+
content: Text('Enter some value to search'),
135+
));
136+
}
132137
}
133138
},
134139
);

lib/web.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class File {
55
File(this.path) {
66
throw PlatformException(
77
code: 'Platform Exception',
8-
message: 'ImageViewer.file does not support in web',
8+
message: 'TextViewer.file does not support in web',
99
);
1010
}
1111
String readAsStringSync() {
1212
throw PlatformException(
1313
code: 'Platform Exception',
14-
message: 'ImageViewer.file does not support in web');
14+
message: 'TextViewer.file does not support in web');
1515
}
1616
}

pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_text_viewer
22
description: Flutter textviewer package provide easy and simple way to load and search text from assets,file and even direct input string.
3-
version: 0.0.5
3+
version: 0.0.6
44
homepage: https://github.com/CodingWithTashi/flutter_text_viewer
55
issue_tracker: https://github.com/CodingWithTashi/flutter_text_viewer/issues
66

@@ -15,6 +15,6 @@ dependencies:
1515
dev_dependencies:
1616
flutter_test:
1717
sdk: flutter
18-
flutter_lints: ^2.0.0
18+
flutter_lints: ^2.0.1
1919

2020
flutter:

0 commit comments

Comments
 (0)