-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ManelRosPuig/2.1.0
Update to new Standard Anchor and Controller
- Loading branch information
Showing
9 changed files
with
136 additions
and
630 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include: package:flutter_lints/flutter.yaml | ||
include: package:flutter_lints/flutter.yaml |
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,88 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:standard_searchbar/new/standard_search_bar.dart'; | ||
import 'package:standard_searchbar/new/standard_search_controller.dart'; | ||
|
||
/// If there is no StandardSearchController passed in the constructor of | ||
/// StandardSearchBar, then a default one will be created. | ||
class StandardSearchAnchor extends StatefulWidget { | ||
const StandardSearchAnchor({ | ||
super.key, | ||
this.controller, | ||
required this.searchBar, | ||
}); | ||
|
||
final StandardSearchController? controller; | ||
final StandardSearchBar searchBar; | ||
|
||
@override | ||
State<StandardSearchAnchor> createState() => StandardSearchAnchorState(); | ||
} | ||
|
||
class StandardSearchAnchorState extends State<StandardSearchAnchor> { | ||
late final StandardSearchController controller; | ||
final unfocus = [false, false]; | ||
final layerLink = LayerLink(); | ||
OverlayEntry? entry; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
if (widget.controller != null) { | ||
controller = widget.controller!; | ||
} else { | ||
controller = StandardSearchController(); | ||
} | ||
controller.anchor = this; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return TapRegion( | ||
onTapInside: (e) { | ||
controller.open(); | ||
}, | ||
onTapOutside: (e) { | ||
controller.close(); | ||
}, | ||
child: CompositedTransformTarget( | ||
link: layerLink, | ||
child: widget.searchBar, | ||
), | ||
); | ||
} | ||
|
||
void clear() { | ||
controller.clear(); | ||
} | ||
|
||
void open() { | ||
if (entry != null) return; | ||
final renderBox = context.findRenderObject() as RenderBox; | ||
final offset = renderBox.localToGlobal(Offset.zero); | ||
entry = OverlayEntry(builder: (_) { | ||
return Positioned( | ||
left: offset.dx, | ||
top: offset.dy + renderBox.size.height + 16, | ||
width: renderBox.size.width, | ||
child: CompositedTransformFollower( | ||
link: layerLink, | ||
showWhenUnlinked: false, | ||
offset: Offset(0, renderBox.size.height), | ||
child: Container( | ||
height: 200, | ||
color: Colors.red, | ||
), | ||
), | ||
); | ||
}); | ||
Overlay.of(context).insert(entry!); | ||
} | ||
|
||
void close() { | ||
if (entry != null) { | ||
entry!.remove(); | ||
entry = null; | ||
} | ||
} | ||
} |
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,15 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class StandardSearchBar extends StatelessWidget { | ||
const StandardSearchBar({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
width: 360, | ||
height: 56, | ||
color: Colors.yellow, | ||
padding: const EdgeInsets.symmetric(horizontal: 16), | ||
); | ||
} | ||
} |
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,24 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:standard_searchbar/new/standard_search_anchor.dart'; | ||
|
||
class StandardSearchController extends TextEditingController { | ||
// Anchor attached to this controller | ||
StandardSearchAnchorState? _anchor; | ||
set anchor(StandardSearchAnchorState anchor) => _anchor = anchor; | ||
|
||
void open() => _anchor?.open(); | ||
void close() => _anchor?.close(); | ||
|
||
@override | ||
void clear() { | ||
super.clear(); | ||
_anchor?.clear(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
_anchor?.close(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.