// “newslist”: [
// {
// “id”: 26754,
// “author”: “test33”,
// “pubDate”: “2013-09-17 16:49:50.0”,
// “title”: “asdfa”,
// “authorid”: 253469,
// “commentCount”: 0,
// “type”: 4
// }
// ],
// “notice”: {
// “replyCount”: 0,
// “msgCount”: 0,
// “fansCount”: 0,
// “referCount”: 0
// }
//}
print(‘Debug NEWS_LIST=====: $data’);
if (data != null && data.isNotEmpty) {
Map<String, dynamic> map = json.decode(data);
List _newsList = map[‘newslist’];
if (!mounted) return;
setState(() {
if (isLoadMore) {
//加载更多
newsList.addAll(_newsList);
} else {
newsList = _newsList;
}
});
}
});
});
}
});
}
@override
Widget build(BuildContext context) {
//没有登录就去登录
if (!isLogin) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(‘由于openapi限制,必须登录才能获取资讯!’),
InkWell(
onTap: () async {
final result = await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => LoginWebPage()));
if (result != null && result == ‘refresh’) {
//登录成功
eventBus.fire(LoginEvent());
}
},
child: Text(‘去登录’),
),
],
),
);
}
//下拉刷新
Future _pullToRefresh() async {
curPage = 1;
getNewsList(false);
return null;
}
//下拉刷新的
return RefreshIndicator(
onRefresh: _pullToRefresh,
child: buildListView(),
);
}
Widget buildListView() {
if (newsList == null) {
getNewsList(false);
//返回转圈圈
return CupertinoActivityIndicator();
}
return ListView.builder(
controller: _controller,
itemCount: newsList.length,
itemBuilder: (context, index) {
return NewsListItem(newsList: newsList[index]);
});
}
}
NewsListItem.dart单个条目的布局,点击以后会跳转到详情的信息
import ‘package:flutter/material.dart’;
import ‘package:flutterapp2/pages/news_detail_page.dart’;
/**
- 新闻主界面Item的布局
*/
class NewsListItem extends StatelessWidget {
final Map<String, dynamic> newsList;
//构造函数
NewsListItem({this.newsList});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => NewsDetailPage(id: newsList[‘id’])));
},
child: Container(
margin: const EdgeInsets.only(left: 20.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Color(0xffaaaaaa),
width: 1.0,
),
),
),
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0, right: 20.0),
child: Column(
children: [
Text(’${newsList[‘title’]}’,
style: TextStyle(fontSize: 20.0, fontWeight: FontWeig
ht.bold),
maxLines: 2,//最多2行
overflow: TextOverflow.ellipsis,//超出是…
),
SizedBox(height:10.0),
Row(
//分布在左右两边
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
‘@${newsList[‘author’]} ${newsList[‘pubDate’].toString().split(’ ‘)[0]}’,
style: TextStyle(color: Color(0xaaaaaaaa), fontSize: 14.0),
),
Row(
children: [
Icon(Icons.message),
右两边
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
‘@${newsList[‘author’]} ${newsList[‘pubDate’].toString().split(’ ‘)[0]}’,
style: TextStyle(color: Color(0xaaaaaaaa), fontSize: 14.0),
),
Row(
children: [
Icon(Icons.message),