Skip to content

Instantly share code, notes, and snippets.

@Nidal-Bakir
Forked from slightfoot/main.dart
Last active January 13, 2023 10:22
Show Gist options
  • Save Nidal-Bakir/f6f9b54afb7e26ae20c673f20938418a to your computer and use it in GitHub Desktop.
Save Nidal-Bakir/f6f9b54afb7e26ae20c673f20938418a to your computer and use it in GitHub Desktop.
A base line to code an overlay in flutter. like search overlay on Text-Field
// MIT License
//
// Copyright (c) 2023 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Home(),
);
}
}
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Padding(
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 24.0),
child: SearchField(),
),
);
}
}
class SearchField extends StatefulWidget {
const SearchField({super.key});
@override
State<SearchField> createState() => _SearchFieldState();
}
class _SearchFieldState extends State<SearchField> {
late final _controller = TextEditingController()
..addListener(_onFocusOrTextChanged);
late final _focusNode = FocusNode()..addListener(_onFocusOrTextChanged);
final LayerLink _layerLink = LayerLink();
OverlayEntry? _overlayEntry;
final _widthNotifier = ValueNotifier(0.0);
void _onFocusOrTextChanged() {
if (!_focusNode.hasFocus || _controller.text.trim().isEmpty) {
if (_overlayEntry == null) {
return;
}
_overlayEntry?.remove();
_overlayEntry?.dispose();
_overlayEntry = null;
return;
}
if (_overlayEntry != null) {
return;
}
_overlayEntry = OverlayEntry(
builder: (context) {
return SearchSuggestions(
controller: _controller,
layerLink: _layerLink,
width: _widthNotifier,
);
},
);
Overlay.of(context)!.insert(_overlayEntry!);
}
@override
void dispose() {
_controller.removeListener(_onFocusOrTextChanged);
_controller.dispose();
_focusNode.removeListener(_onFocusOrTextChanged);
_focusNode.dispose();
_widthNotifier.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return CompositedTransformTarget(
link: _layerLink,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
scheduleMicrotask(() => _widthNotifier.value = constraints.maxWidth);
return TextField(
controller: _controller,
focusNode: _focusNode,
);
},
),
);
}
}
class SearchSuggestions extends StatefulWidget {
final TextEditingController controller;
final LayerLink layerLink;
final ValueNotifier<double> width;
const SearchSuggestions({
Key? key,
required this.controller,
required this.layerLink,
required this.width,
}) : super(key: key);
@override
State<SearchSuggestions> createState() => _SearchSuggestionsState();
}
class _SearchSuggestionsState extends State<SearchSuggestions> {
@override
void initState() {
widget.controller.addListener(_onTextChanged);
super.initState();
}
@override
void dispose() {
widget.controller.removeListener(_onTextChanged);
super.dispose();
}
late var _text = widget.controller.text;
void _onTextChanged() {
setState(() {
_text = widget.controller.text;
});
}
@override
Widget build(BuildContext context) {
return CompositedTransformFollower(
link: widget.layerLink,
followerAnchor: Alignment.topLeft,
targetAnchor: Alignment.bottomLeft,
child: Align(
alignment: Alignment.topLeft,
child: Builder(builder: (context) {
return ValueListenableBuilder<double>(
valueListenable: widget.width,
builder: (context, width, child) {
return SizedBox(
width: width,
// height: 200,
child: Material(
type: MaterialType.card,
child: SingleChildScrollView(
child: Column(
children: [
InkWell(
onTap: () {},
child: ListTile(
title: Text(_text),
),
),
],
),
),
),
);
},
);
}),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment