【发布时间】:2023-03-26 20:15:01
【问题描述】:
你好,我现在正在学习颤振,我正在尝试获取 json 数据并将其显示在轮播小部件上,下面是完整的代码,以便您了解发生了什么
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']}"),
),
),
);
},
);
}
}
目前数据是代码在上面定义的 listitem 类中运行良好,但是我希望数据显示在如下所示的轮播部分上
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),
),
);
有什么帮助吗?我在网上搜索了很多,我看不到我错过了什么
【问题讨论】:
标签: json flutter flutter-layout rest