Last active
January 11, 2023 13:52
-
-
Save kumar-aakash86/a5950f2da95d58e32dc9176433d9bcb3 to your computer and use it in GitHub Desktop.
Flutter - Search using provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is distributed under the MIT License. | |
// Copyright (c) 2019 Remi Rousselet. | |
// You can find the original at https://github.com/rrousselGit/provider. | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert' as convert; | |
void main() { | |
runApp( | |
const MyApp(), | |
); | |
} | |
class ApiCall { | |
static getAllProducts() async { | |
final url = Uri.https('dummyjson.com', '/products'); | |
return await _getProducts(url); | |
} | |
static search(String val) async { | |
final url = Uri.https( | |
'dummyjson.com', | |
'/products/search', | |
{'q': val}, | |
); | |
return await _getProducts(url); | |
} | |
static _getProducts(Uri url) async { | |
print(url); | |
// Await the HTTP GET response, then decode the | |
// JSON data it contains. | |
final response = await http.get(url); | |
if (response.statusCode == 200) { | |
final jsonResponse = convert.jsonDecode(response.body); | |
final items = jsonResponse['products']; | |
print('Number of books about HTTP: $items'); | |
return items; | |
} else { | |
print('Request failed with status: ${response.statusCode}.'); | |
} | |
} | |
} | |
class SearchProvider with ChangeNotifier, DiagnosticableTreeMixin { | |
List<dynamic>? _items; | |
List<dynamic>? get items => _items; | |
void searchItems(String val) async { | |
_items = await ApiCall.search(val); | |
notifyListeners(); | |
} | |
void getAll() async { | |
_items = await ApiCall.getAllProducts(); | |
notifyListeners(); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
SearchProvider _provider = SearchProvider(); | |
@override | |
void initState() { | |
super.initState(); | |
WidgetsBinding.instance.addPostFrameCallback((_) { | |
_provider.getAll(); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// final c = context.watch<Counter>(); | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Provider example'), | |
), | |
body: ChangeNotifierProvider<SearchProvider>( | |
create: (_) => _provider, | |
child: Column( | |
children: [ | |
Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 15.0), | |
child: TextField( | |
onSubmitted: (val) { | |
if(val.length > 0){ | |
_provider.searchItems(val); | |
} | |
else { | |
_provider.getAll(); | |
} | |
}, | |
decoration: const InputDecoration( | |
labelText: 'Search', | |
suffixIcon: Icon(Icons.search), | |
), | |
), | |
), | |
Expanded( | |
child: | |
Consumer<SearchProvider>(builder: (context, provider, w) { | |
if (provider.items != null && provider.items!.isNotEmpty) { | |
print(provider.items); | |
return SearchList(list: provider.items); | |
} | |
return const Center( | |
child: Text("Nothing found"), | |
); | |
}), | |
) | |
], | |
)), | |
); | |
} | |
} | |
class SearchList extends StatelessWidget { | |
final List<dynamic>? list; | |
const SearchList({Key? key, required this.list}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: list!.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
leading: CircleAvatar( | |
backgroundImage: NetworkImage( | |
list![index]["thumbnail"], | |
)), | |
title: Text(list![index]["title"]), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment