Open kritsanut opened 3 years ago
// JSON SAMPLE //
{ "id": 1, "title": "xxxxx", "image_cover": "http://127.0.0.1:8000/news_images/cover_yNyS3tJ.jpg", "image_detail": "http://127.0.0.1:8000/news_images/image001.jpg", "description": "xxxxxxxx", "created_date": "2020-12-08T11:03:05+07:00", "author": "xxxxx, "added_by": 1 },
import 'package:flutter/material.dart'; import 'package:gscarousel/gscarousel.dart'; import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:http/http.dart';
// News Main screen class NewsCarousel extends StatefulWidget { @override _NewsCarouselState createState() => _NewsCarouselState(); }
class _NewsCarouselState extends State {
List posts = new List();
@override void initState() { super.initState(); }
@override Widget build(BuildContext context) { double screenHeight = MediaQuery.of(context).size.height; return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( body: new Container( child: InkWell( onTap: () => print("Container pressed"), // handle your onTap here child: Container( height: screenHeight / 3, child: FutureBuilder<List>(
future: fetchImageList(),
builder: (
context,
snapshot,
) {
if (snapshot.hasData) {
List list = snapshot.data;
List listImages = list
.map(
(index) => new NetworkImage(index.images),
)
.toList();
return GSCarousel(
images: listImages,
indicatorSize: const Size.square(8.0),
indicatorActiveSize: const Size(18.0, 8.0),
indicatorColor: Colors.white,
indicatorActiveColor: Colors.redAccent,
animationCurve: Curves.easeIn,
contentMode: BoxFit.cover,
);
}
return CircularProgressIndicator();
},
),
),
), //
),
),
);
}
}
//Services Future<List> fetchImageList() async {
final response = await http.get('http://127.0.0.1:8000/api/v1/news');
String logResponse = response.statusCode.toString();
if (response.statusCode == 200) {
print('ResponseStatusCode: $logResponse'); // Check Status Code = 200
print('ResponseBody: ' + response.body); // Read Data in Array
List responseJson = json.decode(response.body);
if (responseJson.length >= 8) {
responseJson.length = 8;
}
return responseJson.map((m) => new ImageList.fromJson(m)).toList();
} else {
throw Exception('error :(');
}
}
// Models class ImageList { final String images; ImageList({ this.images, }); factory ImageList.fromJson(Map<String, dynamic> json) { return ImageList( images: json['image_cover'], ); } }