代码:
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_project/model/category_model.dart';
import 'dart:convert';
import 'package:flutter_project/service/service_method.dart';
class LeftCategory extends StatefulWidget {
LeftCategory({Key key}) : super(key: key);
@override
_LeftCategoryState createState() => _LeftCategoryState();
}
class _LeftCategoryState extends State<LeftCategory> {
List leftData;
@override
void initState() {
_getCategoryData();
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
ScreenUtil().setWidth(180),
child: ListView.builder(
itemBuilder: (context,index){
return _leftInkwell(index);
},
itemCount: leftData.length,
)
);
}
Widget _leftInkwell(int leftIndex){
return InkWell(
onTap: (){
},
child: Container(
height: ScreenUtil().setHeight(100),
padding: EdgeInsets.only(top: 20.0,left: 10.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide( 1,color: Colors.black12),
),
color: Colors.white
),
child: Text(
leftData[leftIndex].mallCategoryName,style: TextStyle(fontSize: ScreenUtil().setSp(28)),
),
),
);
}
//请求分类数据
void _getCategoryData() {
getCategoryContent().then((val){
var data = json.decode(val);
CategoryListModel categoryListModel = CategoryListModel.fromJson(data);
setState(() {//刷新整个UI
leftData = categoryListModel.data;
});
});
}
}
总结:
//初始化在
void initState() {
super.initState();
}
相当于OC 的init
刷新整个UI 是
setState(() {
});
相当于OC的reload