Skip to content

Commit

Permalink
Merge pull request #2 from ManelRosPuig/2.1.0
Browse files Browse the repository at this point in the history
Update to new Standard Anchor and Controller
  • Loading branch information
ManelRosPuig authored Sep 22, 2023
2 parents c5fc7cc + 07ae114 commit f5dcc82
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 630 deletions.
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:flutter_lints/flutter.yaml
include: package:flutter_lints/flutter.yaml
27 changes: 8 additions & 19 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';

import 'package:standard_searchbar/standard_searchbar.dart';
import 'package:standard_searchbar/new/standard_search_anchor.dart';
import 'package:standard_searchbar/new/standard_search_bar.dart';

// import 'package:standard_searchbar/standard_searchbar.dart';

void main() => runApp(const MyApp());

Expand All @@ -16,24 +19,10 @@ class MyApp extends StatelessWidget {
),
body: const SizedBox(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 50),
StandardSearchBar(
width: 360,
suggestions: [
'apple',
'banana',
'melon',
'orange',
'pineapple',
'strawberry',
'watermelon'
],
),
SizedBox(height: 200),
],
child: Center(
child: StandardSearchAnchor(
searchBar: StandardSearchBar(),
),
),
),
// backgroundColor: Colors.black12,
Expand Down
88 changes: 88 additions & 0 deletions lib/new/standard_search_anchor.dart
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;
}
}
}
15 changes: 15 additions & 0 deletions lib/new/standard_search_bar.dart
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),
);
}
}
24 changes: 24 additions & 0 deletions lib/new/standard_search_controller.dart
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();
}
}
60 changes: 0 additions & 60 deletions lib/standard_icon.dart

This file was deleted.

Loading

0 comments on commit f5dcc82

Please sign in to comment.