Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the cancellation widget's area next to the search bar more dynamic. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 51 additions & 37 deletions lib/flappy_search_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class SearchBarController<T> {
CancelableOperation _cancelableOperation;
int minimumChars;

void setTextController(TextEditingController _searchQueryController, minimunChars) {
void setTextController(
TextEditingController _searchQueryController, minimunChars) {
this._searchQueryController = _searchQueryController;
this.minimumChars = minimunChars;
}
Expand Down Expand Up @@ -215,38 +216,43 @@ class SearchBar<T> extends StatefulWidget {
/// Set a padding on the list
final EdgeInsetsGeometry listPadding;

SearchBar({
Key key,
@required this.onSearch,
@required this.onItemFound,
this.searchBarController,
this.minimumChars = 3,
this.debounceDuration = const Duration(milliseconds: 500),
this.loader = const Center(child: CircularProgressIndicator()),
this.onError,
this.emptyWidget = const SizedBox.shrink(),
this.header,
this.placeHolder,
this.icon = const Icon(Icons.search),
this.hintText = "",
this.hintStyle = const TextStyle(color: Color.fromRGBO(142, 142, 147, 1)),
this.iconActiveColor = Colors.black,
this.textStyle = const TextStyle(color: Colors.black),
this.cancellationWidget = const Text("Cancel"),
this.onCancelled,
this.suggestions = const [],
this.buildSuggestion,
this.searchBarStyle = const SearchBarStyle(),
this.crossAxisCount = 1,
this.shrinkWrap = false,
this.indexedScaledTileBuilder,
this.scrollDirection = Axis.vertical,
this.mainAxisSpacing = 0.0,
this.crossAxisSpacing = 0.0,
this.listPadding = const EdgeInsets.all(0),
this.searchBarPadding = const EdgeInsets.all(0),
this.headerPadding = const EdgeInsets.all(0),
}) : super(key: key);
/// The space in the width of the space bar to
/// allocate for the cancellation widget.
final double cancellationWidgetSpaceWidth;

SearchBar(
{Key key,
@required this.onSearch,
@required this.onItemFound,
this.searchBarController,
this.minimumChars = 3,
this.debounceDuration = const Duration(milliseconds: 500),
this.loader = const Center(child: CircularProgressIndicator()),
this.onError,
this.emptyWidget = const SizedBox.shrink(),
this.header,
this.placeHolder,
this.icon = const Icon(Icons.search),
this.hintText = "",
this.hintStyle = const TextStyle(color: Color.fromRGBO(142, 142, 147, 1)),
this.iconActiveColor = Colors.black,
this.textStyle = const TextStyle(color: Colors.black),
this.cancellationWidget = const Text("Cancel"),
this.onCancelled,
this.suggestions = const [],
this.buildSuggestion,
this.searchBarStyle = const SearchBarStyle(),
this.crossAxisCount = 1,
this.shrinkWrap = false,
this.indexedScaledTileBuilder,
this.scrollDirection = Axis.vertical,
this.mainAxisSpacing = 0.0,
this.crossAxisSpacing = 0.0,
this.listPadding = const EdgeInsets.all(0),
this.searchBarPadding = const EdgeInsets.all(0),
this.headerPadding = const EdgeInsets.all(0),
this.cancellationWidgetSpaceWidth})
: super(key: key);

@override
_SearchBarState createState() => _SearchBarState<T>();
Expand All @@ -268,7 +274,8 @@ class _SearchBarState<T> extends State<SearchBar<T>>
searchBarController =
widget.searchBarController ?? SearchBarController<T>();
searchBarController.setListener(this);
searchBarController.setTextController(_searchQueryController, widget.minimumChars);
searchBarController.setTextController(
_searchQueryController, widget.minimumChars);
}

@override
Expand Down Expand Up @@ -373,6 +380,12 @@ class _SearchBarState<T> extends State<SearchBar<T>>

@override
Widget build(BuildContext context) {
double _getCancellationWidth() {
return (widget.cancellationWidgetSpaceWidth != null
? widget.cancellationWidgetSpaceWidth
: MediaQuery.of(context).size.width * .2);
}

final widthMax = MediaQuery.of(context).size.width;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
Expand All @@ -387,7 +400,9 @@ class _SearchBarState<T> extends State<SearchBar<T>>
Flexible(
child: AnimatedContainer(
duration: Duration(milliseconds: 200),
width: _animate ? widthMax * .8 : widthMax,
width: _animate
? widthMax - _getCancellationWidth()
: widthMax,
decoration: BoxDecoration(
borderRadius: widget.searchBarStyle.borderRadius,
color: widget.searchBarStyle.backgroundColor,
Expand Down Expand Up @@ -421,8 +436,7 @@ class _SearchBarState<T> extends State<SearchBar<T>>
duration: Duration(milliseconds: _animate ? 1000 : 0),
child: AnimatedContainer(
duration: Duration(milliseconds: 200),
width:
_animate ? MediaQuery.of(context).size.width * .2 : 0,
width: _animate ? _getCancellationWidth() : 0,
child: Container(
color: Colors.transparent,
child: Center(
Expand Down