import 'dart:convert';
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:music/data/GetMomentList.dart';
import 'package:shared_preferences/shared_preferences.dart';
class HomePage extends StatefulWidget {
const HomePage({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return new Page();
}
}
class Page extends State<HomePage> {
var _items = [];
@override
Widget build(BuildContext context) {
return layout(context);
}
@override
void initState() {
super.initState();
getMomentLists();
}
getMomentLists() async {
var apiUrl = "http://47.242.63.216:9527/v1/moment/getList";
SharedPreferences prefs = await SharedPreferences.getInstance();
var tokens = prefs.getString("token");
Map map = {};
map["moment_id"] = 0;
Response result = await Dio().post(apiUrl,
data: map, options: Options(headers: {"x-token": tokens}));
log(result.toString());
Map<String, dynamic> nickname = json.decode(result.toString());
var httpRes = GetMomentList.fromJson(nickname);
if (httpRes.code == 200) {
setState(() {
_items = httpRes.data.list;
log(_items.length.toString());
});
}
}
Widget layout(BuildContext context) {
return new Scaffold(
appBar: buildAppBar(context),
body:
new ListView.builder(itemCount: _items.length, itemBuilder: itemView),
);
}
Widget itemView(BuildContext context, int index) {
List1 model = this._items[index];
return new Container(
color: Color.fromARGB(0x22, 0x49, 0xa9, 0x8d),
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Text('${model.momentInfo.createTime}年${model.userInfo.userId}月',
style: new TextStyle(fontSize: 15.0)),
new Text('(${model.userInfo.userId})',
style: new TextStyle(fontSize: 15.0)),
],
),
new Center(
heightFactor: 6.0,
child: new Text("${model.userInfo.userId}",
style: new TextStyle(fontSize: 17.0)),
)
],
))));
}
Widget buildAppBar(BuildContext context) {
return new AppBar(title: const Text('历史今日'));
}
}
另外一个事例
import 'dart:convert';
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:music/Utils/StringUtil.dart';
import 'package:music/data/GetMomentList.dart';
import 'package:shared_preferences/shared_preferences.dart';
class DynamicRecommendNav extends StatefulWidget {
const DynamicRecommendNav({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return new Page();
}
}
class Page extends State<DynamicRecommendNav> {
var _items = [];
@override
Widget build(BuildContext context) {
if (_items.length == 0) {
return layout1(context);
} else if (_items.length == 3) {
return layout(context);
} else {
return layout(context);
}
}
@override
void initState() {
super.initState();
getMomentLists();
}
getMomentLists() async {
var apiUrl = "http://47.242.63.216:9527/v1/moment/getList";
SharedPreferences prefs = await SharedPreferences.getInstance();
var tokens = prefs.getString("token");
Map map = {};
map["moment_id"] = 0;
Response result = await Dio().post(apiUrl,
data: map, options: Options(headers: {"x-token": tokens}));
log(result.toString());
Map<String, dynamic> nickname = json.decode(result.toString());
var httpRes = GetMomentList.fromJson(nickname);
if (httpRes.code == 200) {
setState(() {
_items = httpRes.data.list;
log(_items.length.toString());
});
}
}
Widget layout(BuildContext context) {
return new Scaffold(
body: Container(
decoration: const BoxDecoration(
color: Color(0xFF1F1F1F),
),
child:
ListView.builder(itemCount: _items.length, itemBuilder: itemView),
),
);
}
Widget layout1(BuildContext context) {
return new Scaffold(
body:
new ListView.builder(
itemCount: _items.length, itemBuilder: itemView1),
);
}
Row dynamicTxtSection(String text) {
return Row(
children: [
Container(
margin: EdgeInsets.only(left: 16, bottom: 17),
child: Text(
text,
style: TextStyle(color: Color(0xFFBBBBBB), fontSize: 16),
),
)
],
);
}
Widget placeSection(String area) {
return Row(
children: [
GestureDetector(
child: Container(
margin: const EdgeInsets.only(left: 15.0),
child: Text(
area,
style: TextStyle(color: Color(0xFF888888)),
),
)),
],
);
}
Widget interactiveSection() {
return Container(
margin: const EdgeInsets.only(top: 25.0, left: 15.0, bottom: 51.0),
child: Row(
children: [
Container(
width: 25.0,
child: Image.asset(
"assets/base_widgets/icon_dynamic_recom_like.png",
fit: BoxFit.fitWidth,
),
),
Container(
margin: const EdgeInsets.only(left: 6.0, right: 24.0),
child: Text(
"996",
style: TextStyle(color: Color(0xFFBBBBBB)),
),
),
Container(
width: 25.0,
child: Image.asset(
"assets/base_widgets/icon_dynamic_recom_Comment.png",
fit: BoxFit.fitWidth,
),
),
Container(
margin: const EdgeInsets.only(left: 6.0, right: 24.0),
child: Text(
"65",
style: TextStyle(color: Color(0xFFBBBBBB)),
),
),
Container(
width: 25.0,
child: Image.asset(
"assets/base_widgets/icon_dynamic_gift_box.png",
fit: BoxFit.fitWidth,
),
),
Container(
margin: const EdgeInsets.only(left: 6.0, right: 24.0),
child: Text(
"1066",
style: TextStyle(color: Color(0xFFBBBBBB)),
),
)
],
),
);
}
List<String> SONGNAME = [
'音乐1',
'音乐2',
'音乐3',
];
Widget dynamicItem(String name) {
return Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
width: 110,
height: 110,
decoration: BoxDecoration(
color: Colors.grey[300],
image: const DecorationImage(
image: NetworkImage(
"https://www.itying.com/images/flutter/3.png"),
fit: BoxFit.fill),
borderRadius: BorderRadius.circular(10)),
margin: const EdgeInsets.only(left: 15.0, bottom: 12.0),
),
],
);
}
Widget dynamicListSection() {
return SizedBox(
height: 130,
child: ListView(
scrollDirection: Axis.horizontal,
children: SONGNAME.map((city) => dynamicItem(city)).toList(),
),
);
}
Widget dynamicListSection2() {
return SizedBox(
height: 130,
child: ListView(
scrollDirection: Axis.horizontal,
children: SONGNAME.map((city) => dynamicItem(city)).toList(),
),
);
}
Widget itemView(BuildContext context, int index) {
List1 model = this._items[index];
return new Column(
children: [
Container(
decoration: const BoxDecoration(
color: Color(0xFF1F1F1F),
),
margin: const EdgeInsets.only(
left: 15.0,
),
padding: const EdgeInsets.only(bottom: 10.0),
child: Row(
children: [
Container(
width: 50,
height: 50,
margin: const EdgeInsets.only(right: 15.0),
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(model.userInfo.avatar),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(50),
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(),
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
'${model.userInfo.nickname}',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Color(0xFFFFFFFF),
fontSize: 17.0),
),
),
Row(
children: [
Container(
padding: const EdgeInsets.only(
left: 8.0, right: 8.0, top: 3.0, bottom: 3.0),
decoration: BoxDecoration(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
bottomLeft: Radius.circular(10.0),
bottomRight: Radius.circular(10.0)),
color: const Color(0xFFBBA57A),
border: Border.all(
width: 1, color: const Color(0xFFBBA57A)),
),
child: const Text(
'認證歌手',
style: TextStyle(
color: Color(0xFF1F1F1F),
fontSize: 12,
fontWeight: FontWeight.bold),
),
),
Container(
padding: const EdgeInsets.only(
top: 3.0, bottom: 3.0, left: 8.0, right: 8.0),
margin:
const EdgeInsets.only(left: 10.0, right: 10.0),
decoration: const BoxDecoration(
color: Color(0xFFBBBBBB),
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
child: Row(
children: [
Image.asset(
'assets/base_widgets/icon_dynamic_recom_male.png',
width: 13,
fit: BoxFit.fitWidth,
),
Container(
margin: const EdgeInsets.only(left: 5.0),
child: Text(StringUtils.getAge(
model.userInfo.birthday)))
],
),
),
Container(
child: Image.asset(
'assets/base_widgets/icon_dynamic_recom_grade.png',
width: 50,
fit: BoxFit.fitWidth,
),
),
],
),
],
),
),
Container(
padding: const EdgeInsets.only(bottom: 8.0),
child: Column(
children: <Widget>[
GestureDetector(
onTap: () {
},
child: Container(
width: 35.0,
child: Image.asset(
"assets/base_widgets/icon_grey_more.png",
fit: BoxFit.fitWidth,
)),
),
Text(''),
],
),
),
],
),
),
dynamicTxtSection(model.momentInfo.text),
dynamicListSection(),
dynamicListSection2(),
placeSection(model.userInfo.area),
interactiveSection(),
],
);
}
Widget itemView1(BuildContext context, int index) {
List1 model = this._items[index];
return new Container(
color: Color.fromARGB(0x22, 0x49, 0xa9, 0x8d),
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Text(
'${model.momentInfo.userId}年${model.userInfo.userId}月',
style: new TextStyle(fontSize: 15.0)),
new Text('(${model.userInfo.userId})',
style: new TextStyle(fontSize: 15.0)),
],
),
new Center(
heightFactor: 6.0,
child: new Text("${model.userInfo.userId}",
style: new TextStyle(fontSize: 17.0)),
)
],
))));
}
}
|