您好,我正在使用flutter开发instagram克隆,对于供稿,我希望图像以水平卡轮播视图显示在您关注的用户的帖子中
这是当前的供稿代码:
import 'package:flutter/material.dart';
import 'image_post.dart';
import 'dart:async';
import 'package:async/async.dart';
import 'main.dart';
import 'dart:io';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class Feed extends StatefulWidget {
_Feed createState() => new _Feed();
}
class _Feed extends State<Feed> {
List<ImagePost> feedData;
@override
void initState() {
super.initState();
this._loadFeed();
}
buildFeed() {
if (feedData != null) {
return new ListView(
children: feedData,
);
} else {
return new Container(
alignment: FractionalOffset.center,
child: new CircularProgressIndicator());
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('InstaGroove',
style: const TextStyle(
fontFamily: "Billabong", color: Colors.black, fontSize: 35.0)),
centerTitle: true,
backgroundColor: Colors.white,
),
body: new RefreshIndicator(
onRefresh: _refresh,
child: buildFeed(),
),
);
}
Future<Null> _refresh() async {
await _getFeed();
setState(() {
});
return;
}
_loadFeed() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String json = prefs.getString("feed");
if (json != null) {
List<Map<String, dynamic>> data =
jsonDecode(json).cast<Map<String, dynamic>>();
List<ImagePost> listOfPosts = _generateFeed(data);
setState(() {
feedData = listOfPosts;
});
} else {
_getFeed();
}
}
_getFeed() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String userId = googleSignIn.currentUser.id.toString();
var url =
'https://us-central1-mp-rps.cloudfunctions.net/getFeed?uid=' + userId;
var httpClient = new HttpClient();
List<ImagePost> listOfPosts;
String result;
try {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
String json = await response.transform(utf8.decoder).join();
prefs.setString("feed", json);
List<Map<String, dynamic>> data =
jsonDecode(json).cast<Map<String, dynamic>>();
listOfPosts = _generateFeed(data);
} else {
result =
'Error getting a feed:\nHttp status ${response.statusCode}';
}
} catch (exception) {
result =
'Failed invoking the getFeed function. Exception: $exception';
}
print(result);
setState(() {
feedData = listOfPosts;
});
}
List<ImagePost> _generateFeed(List<Map<String, dynamic>> feedData) {
List<ImagePost> listOfPosts = [];
for (var postData in feedData) {
listOfPosts.add(new ImagePost.fromJSON(postData));
}
return listOfPosts;
}
}
我已经在google和youtube上进行了一些研究,但我不知道如何将卡片轮播添加到我的应用的供稿部分。我还很陌生,所以任何帮助都会很棒!提前致谢!! :)
答案 0 :(得分:1)
尝试此代码。这是样本图像轮播屏幕。目前,已使用硬编码的图像列表,您可以基于api调用中的图像列表来创建Image.network()
的动态列表。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class TestImageCarousel extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _TestImageCarousel();
}
}
class _TestImageCarousel extends State<TestImageCarousel> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Material(
child: Stack(
children: <Widget>[
PageView(
onPageChanged: (id) {
//code to handle page change
},
scrollDirection: Axis.horizontal,
children: getImageList(),
),
],
));
}
}
/*
Get image list to populate in carousel
*/
getImageList() {
return <Widget>[
Padding(
padding: EdgeInsets.only(left: 1, right: 1),
child: Image.network("https://www.gstatic.com/webp/gallery/1.jpg",
fit: BoxFit.fill),
),
Padding(
padding: EdgeInsets.only(left: 1, right: 1),
child: Image.network("https://www.gstatic.com/webp/gallery/2.jpg",
fit: BoxFit.fill),
),
Padding(
padding: EdgeInsets.only(left: 1, right: 1),
child: Image.network("https://www.gstatic.com/webp/gallery/3.jpg",
fit: BoxFit.fill),
),
Padding(
padding: EdgeInsets.only(left: 1, right: 1),
child: Image.network("https://www.gstatic.com/webp/gallery/4.jpg",
fit: BoxFit.fill),
)
];
}
现在,您可以进行网络调用以获取提要图像列表并将列表绑定到Image.network("src")
。还可以添加页面控制器来获得自动轮播效果。在InkWell()
中添加此图像以获取onTap侦听器。