Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import 'dart:convert';
  2. import 'dart:collection';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:hitech_v2/products.dart';
  6. import 'package:http/http.dart' as http;
  7.  
  8. void main() => runApp(MainApp());
  9.  
  10. class MainApp extends StatelessWidget {
  11. @override
  12. Widget build(BuildContext context) {
  13. // TODO: implement build
  14. return MaterialApp(
  15. theme: ThemeData(
  16. primarySwatch: Colors.blue,
  17. ),
  18. home: MainFetchData(),
  19. );
  20. }
  21. }
  22.  
  23. class MainFetchData extends StatefulWidget {
  24. @override
  25. _MainFetchDataState createState() => _MainFetchDataState();
  26.  
  27. }
  28. class _MainFetchDataState extends State<MainFetchData> {
  29.  
  30. List<Products> list = List();
  31. var isLoading = false;
  32. var url = "my url api";
  33. var action = '';
  34.  
  35. _fetchData() async {
  36. setState(() {
  37. isLoading = true;
  38. });
  39. final response = await http.get(url+"?action=search&term=display");
  40. if (response.statusCode == 200) {
  41. list = (json.decode(response.body) as List)
  42. .map((data) => new Products.fromJson(data))
  43. .toList();
  44. setState(() {
  45. isLoading = false;
  46. });
  47. } else {
  48. throw Exception('Impossibile caricare i prodotti');
  49. }
  50. }
  51.  
  52. @override
  53. Widget build(BuildContext context) {
  54. return Scaffold(
  55. appBar: AppBar(
  56. title: Text("Fetch Data JSON"),
  57. ),
  58.  
  59. bottomNavigationBar: Padding(
  60. padding: const EdgeInsets.all(8.0),
  61. child: Row(
  62. children: <Widget>[
  63. Expanded(
  64. child: RaisedButton(
  65. child: new Text("Fetch Data"),
  66. onPressed: _fetchData,
  67. ),
  68. ),
  69. Expanded(
  70. child: TextField(
  71. decoration: InputDecoration(
  72. border: InputBorder.none,
  73. hintText: 'Cosa vuoi cercare?'
  74. ),
  75. onSubmitted: (text) {
  76. setState(() {
  77. });
  78. },
  79. )
  80. )
  81. ],
  82. )
  83. ),
  84. body: isLoading
  85. ? Center(
  86. child: CircularProgressIndicator(),
  87. )
  88. : ListView.builder(
  89. itemCount: list.length,
  90. itemBuilder: (BuildContext context, int index) {
  91. return ListTile(
  92. contentPadding: EdgeInsets.all(10.0),
  93. title: new Text(list[index].name),
  94. trailing: new Image.network(
  95. list[index].image,
  96. fit: BoxFit.cover,
  97. height: 40.0,
  98. width: 40.0,
  99. ),
  100. );
  101. })
  102. );
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement