Advertisement
fahimkamal63

Search Bar Flutter

Dec 12th, 2021
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.08 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. class SearchBarClass extends StatefulWidget {
  4.   const SearchBarClass({Key? key}) : super(key: key);
  5.  
  6.   @override
  7.   _SearchBarClassState createState() => _SearchBarClassState();
  8. }
  9.  
  10. class _SearchBarClassState extends State<SearchBarClass> {
  11.  
  12.   List<String> nameList = [
  13.     'Fahim', 'Kamal', 'Ahmed', 'Sakib', 'Sayem', 'Rakib', 'Mim', 'Shanto',
  14.     'Lafia', 'Mostak', 'Siyam', 'Nayan', 'Moksad'
  15.   ];
  16.  
  17.   List<String>? dataHolder;
  18.  
  19.   @override
  20.   void initState() {
  21.     dataHolder = List.from(nameList);
  22.     super.initState();
  23.   }
  24.  
  25.   @override
  26.   Widget build(BuildContext context) {
  27.     return Scaffold(
  28.       appBar: AppBar(
  29.         title: Text("Scarch Bar"), centerTitle: true,
  30.       ),
  31.       body: Container(
  32.         margin: EdgeInsets.all(10),
  33.         padding: EdgeInsets.all(10),
  34.         child: Column(
  35.           children: [
  36.             TextField(
  37.               // keyboardType: TextInputType.number,
  38.               obscureText: false,
  39.               decoration: InputDecoration(
  40.                 border: OutlineInputBorder(),
  41.                 hintText: 'Scarch Here...',
  42.               ),
  43.               onChanged: (input){
  44.                 setState(() {
  45.                   dataHolder=nameList.where((element) => (
  46.                   element.toLowerCase().contains(input.toLowerCase())
  47.                   )).toList();
  48.                 });
  49.               },
  50.             ),
  51.             Expanded(
  52.               child: Container(
  53.                 height: MediaQuery.of(context).size.height * 0.75,
  54.                 child: ListView.builder(
  55.                     itemCount: dataHolder!.length,
  56.                     itemBuilder: (context, index){
  57.                       return Card(
  58.                         child: ListTile(
  59.                           title: Text(dataHolder![index]),
  60.                           subtitle: Text(dataHolder![index] + "is a Motamuti Boy."),
  61.                         ),
  62.                       );
  63.                     }
  64.                 ),
  65.               ),
  66.             )
  67.           ],
  68.         ),
  69.       ),
  70.     );
  71.   }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement