• flutter 刷新和初始化


    代码:

    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

  • 相关阅读:
    CCLayerColor 用法
    数据存储的常见方式
    rand() 和arc4random()到底应该如何取舍呢
    Cocos2d 中对图片的各种操作
    objectivec 解压缩zip
    设置IE浏览器默认的下载路径
    设置浏览器主页
    得到所有本机上安装的软件名
    取消快捷方式中的箭头图标
    查看本机服务
  • 原文地址:https://www.cnblogs.com/pp-pping/p/12240588.html
Copyright © 2020-2023  润新知