Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:convert';
- import 'dart:collection';
- import 'package:flutter/material.dart';
- import 'package:hitech_v2/products.dart';
- import 'package:http/http.dart' as http;
- void main() => runApp(MainApp());
- class MainApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- // TODO: implement build
- return MaterialApp(
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MainFetchData(),
- );
- }
- }
- class MainFetchData extends StatefulWidget {
- @override
- _MainFetchDataState createState() => _MainFetchDataState();
- }
- class _MainFetchDataState extends State<MainFetchData> {
- List<Products> list = List();
- var isLoading = false;
- var url = "my url api";
- var action = '';
- _fetchData() async {
- setState(() {
- isLoading = true;
- });
- final response = await http.get(url+"?action=search&term=display");
- if (response.statusCode == 200) {
- list = (json.decode(response.body) as List)
- .map((data) => new Products.fromJson(data))
- .toList();
- setState(() {
- isLoading = false;
- });
- } else {
- throw Exception('Impossibile caricare i prodotti');
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text("Fetch Data JSON"),
- ),
- bottomNavigationBar: Padding(
- padding: const EdgeInsets.all(8.0),
- child: Row(
- children: <Widget>[
- Expanded(
- child: RaisedButton(
- child: new Text("Fetch Data"),
- onPressed: _fetchData,
- ),
- ),
- Expanded(
- child: TextField(
- decoration: InputDecoration(
- border: InputBorder.none,
- hintText: 'Cosa vuoi cercare?'
- ),
- onSubmitted: (text) {
- setState(() {
- });
- },
- )
- )
- ],
- )
- ),
- body: isLoading
- ? Center(
- child: CircularProgressIndicator(),
- )
- : ListView.builder(
- itemCount: list.length,
- itemBuilder: (BuildContext context, int index) {
- return ListTile(
- contentPadding: EdgeInsets.all(10.0),
- title: new Text(list[index].name),
- trailing: new Image.network(
- list[index].image,
- fit: BoxFit.cover,
- height: 40.0,
- width: 40.0,
- ),
- );
- })
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement