美文网首页
flutter控件之Http

flutter控件之Http

作者: Mr丶xi先森 | 来源:发表于2018-09-11 16:09 被阅读29次
    
    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;
          });
        });
      }
    }
    
    

    相关文章

      网友评论

          本文标题:flutter控件之Http

          本文链接:https://www.haomeiwen.com/subject/qtmugftx.html