Reputation: 85
hello i am learning flutter now and i am trying to get json data and display it on a carousel widget , below is the full code just so you get an idea of what is going on
import 'package:flutter/material.dart';
import 'package:mares/components/drawer.dart';
import 'package:carousel_pro/carousel_pro.dart';
import 'package:mares/components/homecards.dart';
import 'package:mares/pages/contactus.dart';
import 'package:mares/pages/profile.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Elaph Training',
theme: ThemeData(fontFamily: 'Roboto'),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<List> getSlides() async {
final response = await http.get("http://igh-eg.com/mares/json/slides.php");
return json.decode(response.body);
}
@override
Widget build(BuildContext context) {
Widget imagecarousel = new Container(
height: 200.0,
child: new Carousel(
boxFit: BoxFit.cover,
images: [
AssetImage('assets/slide1.jpg'),
AssetImage('assets/slide2.jpg'),
AssetImage('assets/slide3.jpg'),
],
autoplay: true,
animationCurve: Curves.fastOutSlowIn,
animationDuration: Duration(milliseconds: 2000),
dotSize: 5.0,
dotColor: const Color.fromRGBO(234, 91, 12, 1),
),
);
return Scaffold(
appBar: new AppBar(
backgroundColor: const Color.fromRGBO(32, 102, 174, 1),
title: Text('Elaph Training Center'),
actions: <Widget>[
new IconButton(icon: Icon(Icons.phone_in_talk, color: Colors.white,), onPressed: (){Navigator.of(context).push(new MaterialPageRoute(builder: (context) => new ContactUsPage()));}),//search button
new IconButton(icon: Icon(Icons.supervised_user_circle, color: Colors.white,), onPressed: (){Navigator.of(context).push(new MaterialPageRoute(builder: (context) => new Profilepage()));}),//cart button
],
),
//--the end of the app bar--//
//---start of the drawer---//
drawer: NavDrawer(),
body: new ListView(
children: <Widget>[
new FutureBuilder<List>(
future: getSlides(),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? new ItemList(
list: snapshot.data,
)
: new Center(
child: new CircularProgressIndicator(),
);
},
),
imagecarousel,
SizedBox(height: 10.0,),
HomeCards(),
],
),
);
}
}
class ItemList extends StatelessWidget {
final List list;
ItemList({this.list});
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: list == null ? 0 : list.length,
itemBuilder: (context, i) {
return new Container(
padding: const EdgeInsets.all(10.0),
child: new Card(
child: new ListTile(
title: new Text(list[i]['name']),
leading: new Image.network("http://igh-eg.com/mares/img/${list[i]['photo']}"),
subtitle: new Text("Description : ${list[i]['desc']}"),
),
),
);
},
);
}
}
currently the data is code is working fine with the listitem class defined above however i want the data to be displayed on the carousel part shown below
Widget imagecarousel = new Container(
height: 200.0,
child: new Carousel(
boxFit: BoxFit.cover,
images: [
AssetImage('assets/slide1.jpg'),
AssetImage('assets/slide2.jpg'),
AssetImage('assets/slide3.jpg'),
],
autoplay: true,
animationCurve: Curves.fastOutSlowIn,
animationDuration: Duration(milliseconds: 2000),
dotSize: 5.0,
dotColor: const Color.fromRGBO(234, 91, 12, 1),
),
);
any help please? i searched the internet alot and i cannot see what i am missing
Upvotes: 0
Views: 898
Reputation: 85
Thank you guys but i found the problem is with the version of the widget i am using so i simply followed the below steps
Step 1: i added the version (carousel_pro: ^0.0.13) to pubspec.yaml
Step 2: Created the below dart file
import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
Widget CarouselSlider(items) => SizedBox(
height: 200,
child: Carousel(
boxFit: BoxFit.cover,
images: items,
autoplay: true,
animationCurve: Curves.fastOutSlowIn,
animationDuration: Duration(milliseconds: 1500),
),
);
then retrieved the data using http,get while initiating the state inside the main.dart as follows
class _MyHomePageState extends State<MyHomePage> {
var items = [];
@override
void initState() {
// TODO: implement initState
super.initState();
//get json data function//
getSlides();
}
Future<List> getSlides() async {
final response = await http.get("http://igh-eg.com/mares/json/slides.php");
var result = json.decode(response.body);
result['data'].forEach((data){
setState(() {
items.add(NetworkImage("http://igh-eg.com/mares/img/${data['photo']}"));
});
});
print(json.decode(response.body));
return json.decode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
backgroundColor: const Color.fromRGBO(32, 102, 174, 1),
title: Text('Elaph Training Center'),
actions: <Widget>[
new IconButton(icon: Icon(Icons.phone_in_talk, color: Colors.white,), onPressed: (){Navigator.of(context).push(new MaterialPageRoute(builder: (context) => new ContactUsPage()));}),//search button
new IconButton(icon: Icon(Icons.supervised_user_circle, color: Colors.white,), onPressed: (){Navigator.of(context).push(new MaterialPageRoute(builder: (context) => new Profilepage()));}),//cart button
],
),
//--the end of the app bar--//
//---start of the drawer---//
drawer: NavDrawer(),
body: new ListView(
children: <Widget>[
CarouselSlider(items),
SizedBox(height: 10.0,),
HomeCards(),
],
),
);
}
}
and it worked like a chart , and a better way is to use the networkcachedimage widget from pub.dev website if you are working with huge data and images
Thank you all and special thanks to @Sanket Vekariya for his help
Upvotes: 0
Reputation: 2956
I have implemented similar code with carousel_slider: ^2.2.1
& cached_network_image: ^2.1.0+1
package.
You can try the code like below:
class _HomeScreenState extends State<HomeScreen> {
Future<List<Product>> carouselProducts;
...
// method to fecth data from URL
Future<List<Product>> getSlides() async {
return await apiController.carouselDataFetch();
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
carouselProducts = getSlides();
});
}
Widget in build method:
FutureBuilder<List<Product>>(
future: carouselProducts,
builder: (BuildContext cont, AsyncSnapshot snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.connectionState == ConnectionState.none) {
return Center(
child: Text("Turn-On Mobile Data / WiFi Please!",));
}
if (snapshot.hasError) {
return Center(
child: Text("Something went wrong!",));
}
return CarouselSlider.builder(
options: CarouselOptions(
height: 250,
autoPlay: true,
enableInfiniteScroll: false,
enlargeCenterPage: true,
scrollDirection: Axis.horizontal,
scrollPhysics: const BouncingScrollPhysics(),
enlargeStrategy: CenterPageEnlargeStrategy.height,
autoPlayCurve: Curves.fastLinearToSlowEaseIn,
),
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
Product prod = snapshot.data[index];
return CachedNetworkImage(
imageUrl: prod.productImage,
fit: BoxFit.fill,
useOldImageOnUrlChange: true,
fadeInCurve: Curves.linearToEaseOut,
placeholder: (context, url) => Center(
child: CircularProgressIndicator()),
errorWidget: (context, url, error) =>
Icon(Icons.error),
);
},
);
},
)
FYI:
Change this exemplary code as per your API response data.
Upvotes: 3