Advertisement
FahimHoque

floating search

Dec 17th, 2021
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.88 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_svg/flutter_svg.dart';
  3. import 'package:material_floating_search_bar/material_floating_search_bar.dart';
  4.  
  5. class SearchPage extends StatefulWidget {
  6.   const SearchPage({Key? key}) : super(key: key);
  7.  
  8.   @override
  9.   _SearchPageState createState() => _SearchPageState();
  10. }
  11.  
  12. class _SearchPageState extends State<SearchPage> {
  13.   final controller = FloatingSearchBarController();
  14.   late FocusNode _focusNode;
  15.   @override
  16.   void initState() {
  17.     super.initState();
  18.     _focusNode = FocusNode();
  19.   }
  20.  
  21.   @override
  22.   Widget build(BuildContext context) {
  23.     return SafeArea(
  24.       child: Scaffold(
  25.         body: Center(
  26.           child: buildFloatingSearchBar(),
  27.         ),
  28.       ),
  29.     );
  30.   }
  31.  
  32.   Widget buildFloatingSearchBar() {
  33.     final isPortrait =
  34.         MediaQuery.of(context).orientation == Orientation.portrait;
  35.  
  36.     return FloatingSearchBar(
  37.       hint: 'Search...',
  38.       scrollPadding: const EdgeInsets.only(top: 16, bottom: 56),
  39.       transitionDuration: const Duration(milliseconds: 800),
  40.       transitionCurve: Curves.easeInOut,
  41.       physics: const BouncingScrollPhysics(),
  42.       axisAlignment: isPortrait ? 0.0 : -1.0,
  43.       openAxisAlignment: 0.0,
  44.       width: isPortrait ? 600 : 500,
  45.       debounceDelay: const Duration(milliseconds: 500),
  46.       onQueryChanged: (query) {
  47.         print('Query changed: $query');
  48.       },
  49.       onFocusChanged: (hasFocus) {
  50.         print('Focus changed: $hasFocus');
  51.       },
  52.       transition: CircularFloatingSearchBarTransition(),
  53.       actions: [
  54.         FloatingSearchBarAction(
  55.           showIfOpened: false,
  56.           child: CircularButton(
  57.             icon: const Icon(Icons.place),
  58.             onPressed: () {},
  59.           ),
  60.         ),
  61.         FloatingSearchBarAction.searchToClear(
  62.           showIfClosed: false,
  63.         ),
  64.       ],
  65.       builder: (context, transition) {
  66.         return ClipRRect(
  67.           borderRadius: BorderRadius.circular(8),
  68.           child: Material(
  69.             color: Colors.white,
  70.             elevation: 4.0,
  71.             child: _buildSearchHistoryContainer(),
  72.           ),
  73.         );
  74.       },
  75.     );
  76.   }
  77.  
  78.   Widget _buildSearchHistoryContainer() {
  79.     return Container(
  80.       padding: EdgeInsets.all(20),
  81.       height: 200,
  82.       child: Column(
  83.         children: [
  84.           const SizedBox(height: 8),
  85.           _buildSearchHistoryHeader(),
  86.         ],
  87.       ),
  88.     );
  89.   }
  90.  
  91.   Widget _buildSearchHistoryHeader() {
  92.     return Row(
  93.       mainAxisAlignment: MainAxisAlignment.spaceBetween,
  94.       children: [
  95.         Text(
  96.           'Search history',
  97.           style: TextStyle(
  98.             fontSize: 16,
  99.             fontWeight: FontWeight.w500,
  100.           ),
  101.         ),
  102.         const SizedBox(width: 8),
  103.         SvgPicture.asset(
  104.           "assets/icons/trash_small.svg",
  105.         ),
  106.       ],
  107.     );
  108.   }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement