Skip to content

Commit 2654739

Browse files
committed
v 1.0.0
1 parent 17d26bf commit 2654739

File tree

5 files changed

+87
-30
lines changed

5 files changed

+87
-30
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [1.0.0]
2+
* AutoFocus option is now available - when the search bar is opened, the keyboard will pop open and closes when the search bar is closed.
3+
* Added TextStyle option to the TextField
4+
* Added option to close the search bar when suffixIcon is tapped.
5+
* PrefixIcon and SuffixIcon is now Icon not IconData, to allow more granular customization.
6+
17
## [0.0.8]
28
* updated readme with rtl support
39

@@ -8,6 +14,6 @@
814
* Updated Readme
915

1016
## [0.0.2]
11-
* Addded examples
17+
* Added examples
1218

1319
## [0.0.1] - Hello World.

example/main.dart

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class _AppState extends State<App> {
3232
/// In AnimSearchBar widget, the width, textController, onSuffixTap are required properties.
3333
/// You have also control over the suffixIcon, prefixIcon, helpText and animationDurationInMilli
3434
child: AnimSearchBar(
35-
rtl: false,
3635
width: 400,
3736
textController: textController,
3837
onSuffixTap: () {

lib/src/anim_search_widget.dart

+76-24
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,56 @@ import 'dart:math';
44
class AnimSearchBar extends StatefulWidget {
55
/// width - double ,isRequired : Yes
66
/// textController - TextEditingController ,isRequired : Yes
7-
/// rtl - Boolean, isRequired : No
87
/// onSuffixTap - Function, isRequired : Yes
8+
/// rtl - Boolean, isRequired : No
9+
/// autoFocus - Boolean, isRequired : No
10+
/// style - TextStyle, isRequired : No
11+
/// closeSearchOnSuffixTap - bool , isRequired : No
912
/// suffixIcon - IconData ,isRequired : No
1013
/// prefixIcon - IconData ,isRequired : No
1114
/// animationDurationInMilli - int ,isRequired : No
1215
/// helpText - String ,isRequired : No
1316
1417
final double width;
1518
final TextEditingController textController;
16-
final IconData suffixIcon;
17-
final IconData prefixIcon;
19+
final Icon suffixIcon;
20+
final Icon prefixIcon;
1821
final String helpText;
1922
final int animationDurationInMilli;
2023
final onSuffixTap;
2124
final bool rtl;
25+
final bool autoFocus;
26+
final TextStyle style;
27+
final bool closeSearchOnSuffixTap;
28+
29+
const AnimSearchBar({
30+
Key key,
31+
32+
/// The width cannot be null
33+
@required this.width,
2234

23-
const AnimSearchBar(
24-
{Key key,
35+
/// The textController cannot be null
36+
@required this.textController,
37+
this.suffixIcon,
38+
this.prefixIcon,
39+
this.helpText = "Search...",
2540

26-
/// The width cannot be null
27-
@required this.width,
41+
/// The onSuffixTap cannot be null
42+
@required this.onSuffixTap,
43+
this.animationDurationInMilli = 375,
2844

29-
/// The textController cannot be null
30-
@required this.textController,
31-
this.suffixIcon = Icons.close,
32-
this.prefixIcon = Icons.search,
33-
this.helpText = "Search...",
45+
/// make the search bar to open from right to left
46+
this.rtl = false,
3447

35-
/// The onSuffixTap cannot be null
36-
@required this.onSuffixTap,
37-
this.animationDurationInMilli = 375,
48+
/// make the keyboard to show automatically when the searchbar is expanded
49+
this.autoFocus = false,
3850

39-
/// make the search bar to open from right to left
40-
this.rtl = false})
41-
: assert(
51+
/// TextStyle of the contents inside the searchbar
52+
this.style,
53+
54+
/// close the search on suffix tap
55+
this.closeSearchOnSuffixTap = false,
56+
}) : assert(
4257
/// The width cannot be null and double.infinity
4358
width != null && width != double.infinity,
4459

@@ -58,6 +73,7 @@ class _AnimSearchBarState extends State<AnimSearchBar>
5873
with SingleTickerProviderStateMixin {
5974
///initializing the AnimationController
6075
AnimationController _con;
76+
FocusNode focusNode = FocusNode();
6177
@override
6278
void initState() {
6379
super.initState();
@@ -120,15 +136,27 @@ class _AnimSearchBarState extends State<AnimSearchBar>
120136
try {
121137
///trying to execute the onSuffixTap function
122138
widget.onSuffixTap();
139+
140+
///closeSearchOnSuffixTap will execute if it's true
141+
if (widget.closeSearchOnSuffixTap) {
142+
FocusScope.of(context).unfocus();
143+
setState(() {
144+
toggle = 0;
145+
});
146+
}
123147
} catch (e) {
124148
///print the error if the try block fails
125149
print(e);
126150
}
127151
},
128-
child: Icon(
129-
widget.suffixIcon,
130-
size: 20.0,
131-
),
152+
153+
///suffixIcon is of type Icon
154+
child: widget.suffixIcon != null
155+
? widget.suffixIcon
156+
: Icon(
157+
Icons.close,
158+
size: 20.0,
159+
),
132160
),
133161
builder: (context, widget) {
134162
///Using Transform.rotate to rotate the suffix icon when it gets expanded
@@ -158,8 +186,14 @@ class _AnimSearchBarState extends State<AnimSearchBar>
158186
child: TextField(
159187
///Text Controller. you can manipulate the text inside this textField by calling this controller.
160188
controller: widget.textController,
189+
focusNode: focusNode,
161190
cursorRadius: Radius.circular(10.0),
162191
cursorWidth: 2.0,
192+
193+
///style is of type TextStyle, the default is just a color black
194+
style: widget.style != null
195+
? widget.style
196+
: TextStyle(color: Colors.black),
163197
cursorColor: Colors.black,
164198
decoration: InputDecoration(
165199
floatingLabelBehavior: FloatingLabelBehavior.never,
@@ -189,21 +223,39 @@ class _AnimSearchBarState extends State<AnimSearchBar>
189223

190224
///if toggle is 1, which means it's open. so show the back icon, which will close it.
191225
///if the toggle is 0, which means it's closed, so tapping on it will expand the widget.
192-
icon: Icon(
193-
toggle == 1 ? Icons.arrow_back_ios : widget.prefixIcon),
226+
///prefixIcon is of type Icon
227+
icon: widget.prefixIcon != null
228+
? toggle == 1
229+
? Icon(Icons.arrow_back_ios)
230+
: widget.prefixIcon
231+
: Icon(
232+
toggle == 1 ? Icons.arrow_back_ios : Icons.search,
233+
size: 20.0,
234+
),
194235
onPressed: () {
195236
setState(
196237
() {
197238
///if the search bar is closed
198239
if (toggle == 0) {
199240
toggle = 1;
241+
setState(() {
242+
///if the autoFocus is true, the keyboard will pop open, automatically
243+
if (widget.autoFocus)
244+
FocusScope.of(context).requestFocus(focusNode);
245+
});
200246

201247
///forward == expand
202248
_con.forward();
203249
} else {
204250
///if the search bar is expanded
205251
toggle = 0;
206252

253+
///if the autoFocus is true, the keyboard will close, automatically
254+
setState(() {
255+
if (widget.autoFocus)
256+
FocusScope.of(context).unfocus();
257+
});
258+
207259
///reverse == close
208260
_con.reverse();
209261
}

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: anim_search_bar
22
description: A flutter package that has an animated search bar with loads of customization
3-
version: 0.0.8
3+
version: 1.0.0
44
homepage: https://github.com/Imgkl/anim_search_bar
55
environment:
66
sdk: ">=2.7.0 <3.0.0"

test/anim_search_bar_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'package:flutter_test/flutter_test.dart';
2-
3-
import 'package:anim_search_bar/anim_search_bar.dart';
1+
// import 'package:flutter_test/flutter_test.dart';
2+
//
3+
// import 'package:anim_search_bar/anim_search_bar.dart';
44
//
55
// void main() {
66
// test('adds one to input values', () {

0 commit comments

Comments
 (0)