flutter控件之Http


import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class LearnHttp extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return new _LearnHttp();
  }
}
class _LearnHttp extends State<LearnHttp>{
  String httpResult="null";
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body:new Container(
        margin: EdgeInsets.all(20.0),
        padding: EdgeInsets.all(10.0),
        decoration: new BoxDecoration(
          color: Colors.black,
          border:new Border.all(
            color: Colors.yellow,
            width: 10.0,
          ),
        ),
        child: new Center(
          child:new SingleChildScrollView(
            child: new Text(
              httpResult,
              style: new TextStyle(
                  color: Colors.white
              ),
            ),
          ),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Center(
          child: new Text('发送请求',style: new TextStyle(
            fontSize: 10.0,
            color: Colors.yellow
          ),),
        ),
          backgroundColor: Colors.red,
          onPressed: (){
            httpRequest();
          }
      ),
    );
  }

  void httpRequest(){
    var url="http://www.wanandroid.com/article/list/1/json";
    http.get(url).then((response){
      setState(() {
        httpResult=response.body;
      });
    });
  }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容