Advertisement
fahimkamal63

Search bar Flutter

Dec 9th, 2021
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.09 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 = ['Fahim', 'Kamal', 'Ahmed', 'Sakib', 'Sayem', 'Rakib', 'Mim', 'Shanto',
  18.     'Lafia', 'Mostak', 'Siyam', 'Nayan', 'Moksad'];
  19.  
  20.   @override
  21.   Widget build(BuildContext context) {
  22.     return Scaffold(
  23.       appBar: AppBar(
  24.         title: Text("Scarch Bar"), centerTitle: true,
  25.       ),
  26.       body: Container(
  27.         margin: EdgeInsets.all(10),
  28.         padding: EdgeInsets.all(10),
  29.         child: Column(
  30.           children: [
  31.             TextField(
  32.               // keyboardType: TextInputType.number,
  33.               obscureText: false,
  34.               decoration: InputDecoration(
  35.                 border: OutlineInputBorder(),
  36.                 hintText: 'Scarch Here...',
  37.               ),
  38.               onChanged: (input){
  39.                 setState(() {
  40.                   dataHolder=nameList.where((element) => (
  41.                   element.toLowerCase().contains(input.toLowerCase())
  42.                   )).toList();
  43.                 });
  44.               },
  45.             ),
  46.             Expanded(
  47.               child: Container(
  48.                 height: MediaQuery.of(context).size.height * 0.75,
  49.                 child: ListView.builder(
  50.                     itemCount: dataHolder.length,
  51.                     itemBuilder: (context, index){
  52.                       return Card(
  53.                         child: ListTile(
  54.                           title: Text(dataHolder[index]),
  55.                           subtitle: Text(dataHolder[index] + "is a Motamuti Boy."),
  56.                         ),
  57.                       );
  58.                     }
  59.                 ),
  60.               ),
  61.             )
  62.           ],
  63.         ),
  64.       ),
  65.     );
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement