rahmrny

flutter custom dropdown

Feb 24th, 2022 (edited)
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.99 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_svg/flutter_svg.dart';
  3.  
  4. class SearchDropdownButton extends StatefulWidget {
  5.   const SearchDropdownButton({
  6.     Key? key,
  7.     required this.text,
  8.   }) : super(key: key);
  9.  
  10.   final String text;
  11.  
  12.   @override
  13.   State<SearchDropdownButton> createState() => _SearchDropdownButtonState();
  14. }
  15.  
  16. class _SearchDropdownButtonState extends State<SearchDropdownButton> {
  17.   late OverlayEntry _categoryBottomBar;
  18.   bool isDropdownOpened = false;
  19.  
  20.   @override
  21.   Widget build(BuildContext context) {
  22.     return Expanded(
  23.       child: ElevatedButton(
  24.         onPressed: () {
  25.           setState(() {
  26.             if (isDropdownOpened) {
  27.               _categoryBottomBar.remove();
  28.             } else {
  29.               _categoryBottomBar =
  30.                   _buildCategoryBottomBar(widget.text.toLowerCase());
  31.               Overlay.of(context)!.insert(_categoryBottomBar);
  32.             }
  33.  
  34.             isDropdownOpened = !isDropdownOpened;
  35.           });
  36.         },
  37.         style: ButtonStyle(
  38.           padding: (MediaQuery.of(context).size.width < 400)
  39.               ? MaterialStateProperty.all(
  40.                   const EdgeInsets.symmetric(vertical: 10, horizontal: 5))
  41.               : MaterialStateProperty.all(
  42.                   const EdgeInsets.symmetric(vertical: 20)),
  43.           elevation: MaterialStateProperty.all(0),
  44.           backgroundColor: MaterialStateProperty.all(const Color(0xFFF82A5A)),
  45.           shape: MaterialStateProperty.all(
  46.             const RoundedRectangleBorder(
  47.               borderRadius: BorderRadius.only(
  48.                 bottomRight: Radius.circular(8),
  49.                 bottomLeft: Radius.circular(8),
  50.               ),
  51.             ),
  52.           ),
  53.         ),
  54.         child: Row(
  55.           mainAxisAlignment: MainAxisAlignment.center,
  56.           children: [
  57.             Text(
  58.               widget.text,
  59.               style: const TextStyle(
  60.                 fontFamily: "Montserrat",
  61.                 fontWeight: FontWeight.w600,
  62.               ),
  63.             ),
  64.             Icon((isDropdownOpened)
  65.                 ? Icons.keyboard_arrow_down
  66.                 : Icons.keyboard_arrow_up)
  67.           ],
  68.         ),
  69.       ),
  70.     );
  71.   }
  72. }
  73.  
  74. OverlayEntry _buildCategoryBottomBar(String category) {
  75.   return OverlayEntry(
  76.     builder: (context) {
  77.       List<Widget> _getSubCategory() {
  78.         List<Widget> _consumeCategory = [
  79.           _buildSubCategoryButton(context, 'Makanan', 'pizza'),
  80.           _buildSubCategoryButton(context, 'Minuman', 'tea'),
  81.           _buildSubCategoryButton(context, 'Obat', 'tablet'),
  82.         ];
  83.  
  84.         List<Widget> _gadgetCategory = [
  85.           _buildSubCategoryButton(context, 'Top Up\nGame', 'console'),
  86.           _buildSubCategoryButton(context, 'Isi Pulsa', 'token'),
  87.           _buildSubCategoryButton(
  88.               context, 'Token Air\ndan Listrik', 'lamp_and_water'),
  89.         ];
  90.  
  91.         List<Widget> _fashionCategory = [
  92.           _buildSubCategoryButton(context, 'Celana', 'pants'),
  93.           _buildSubCategoryButton(context, 'Pakaian', 'pakaian'),
  94.           _buildSubCategoryButton(context, 'Parfum', 'parfume_bottle'),
  95.         ];
  96.  
  97.         switch (category) {
  98.           case 'consume':
  99.             return _consumeCategory;
  100.           case 'gadget':
  101.             return _gadgetCategory;
  102.           case 'fashion':
  103.             return _fashionCategory;
  104.           default:
  105.             return _consumeCategory;
  106.         }
  107.       }
  108.  
  109.       return Positioned(
  110.         bottom: 0,
  111.         child: Container(
  112.           height: 75,
  113.           width: MediaQuery.of(context).size.width,
  114.           decoration: const BoxDecoration(
  115.               color: Color(0xFFF82A5A),
  116.               borderRadius: BorderRadius.only(
  117.                 topRight: Radius.circular(10),
  118.                 topLeft: Radius.circular(10),
  119.               )),
  120.           child: Row(
  121.             mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  122.             children: _getSubCategory(),
  123.           ),
  124.         ),
  125.       );
  126.     },
  127.   );
  128. }
  129.  
  130. ElevatedButton _buildSubCategoryButton(
  131.     BuildContext context, String text, String icon) {
  132.   return ElevatedButton(
  133.     style: ButtonStyle(
  134.       fixedSize: MaterialStateProperty.all(
  135.         Size(
  136.           MediaQuery.of(context).size.width * 0.30,
  137.           50,
  138.         ),
  139.       ),
  140.       // padding: MaterialStateProperty.all(
  141.       //   (MediaQuery.of(context).size.width < 400)
  142.       //   ? const EdgeInsets.symmetric(vertical: 0)
  143.       //   : const EdgeInsets.symmetric(vertical: 20)
  144.       // ),
  145.       backgroundColor: MaterialStateProperty.all(const Color(0xFFFF665E)),
  146.       elevation: MaterialStateProperty.all(0),
  147.     ),
  148.     onPressed: () {},
  149.     child: Row(
  150.       mainAxisAlignment: MainAxisAlignment.center,
  151.       children: [
  152.         Text(
  153.           text,
  154.           textAlign: TextAlign.center,
  155.         ),
  156.         const SizedBox(width: 7),
  157.         SvgPicture.asset('assets/belankon-icons/$icon.svg',
  158.             color: Colors.white),
  159.       ],
  160.     ),
  161.   );
  162. }
  163.  
Add Comment
Please, Sign In to add comment