【自学Flutter】36 网络操作

本文介绍了一个使用Flutter和Dio库进行网络请求的示例。通过一个简单的应用,演示了如何异步获取网页内容并更新UI。文章展示了如何创建状态管理、按钮点击事件处理以及响应数据的展示。

36 网络操作

1.源代码
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool loading = false;
  String text = "";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 100),
                  child: RaisedButton(
                      child: Text("获取百度首页"),
                      onPressed: loading ? null : () async {
                        setState(() {
                          loading = true;
                          text = "正在请求...";
                        });
                        try {
                          Dio dio = new Dio();
                          Response response=await dio.get("https://www.baidu.com/");
                          text = response.data;
                        } catch (e) {
                          text = "请求失败:$e";
                        } finally {
                          setState(() {
                            loading = false;
                          });
                        }
                      }
                  ),
                ),
                Builder(builder: (context){
                  return Container(
                      width: MediaQuery
                          .of(context)
                          .size
                          .width - 50.0,
                      child: Text(text)
                  );
                })
              ],
            ),
          ),
        ),
      ),
    );
  }
}
2.引用
dio: 2.1.7

在这里插入图片描述

3.解释源代码
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  //是否加载标识
  bool loading = false;
  //显示的文本内容
  String text = "";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 100),
                  child: RaisedButton(
                      child: Text("获取百度首页"),
                      onPressed: loading ? null : () async {
                        setState(() {
                          loading = true;
                          text = "正在请求...";
                        });
                        try {
                          //导包  import 'package:dio/dio.dart';
                          Dio dio = new Dio();
                          Response response=await dio.get("https://www.baidu.com/");
                          text = response.data;
                        } catch (e) {
                          text = "请求失败:$e";
                        } finally {
                          setState(() {
                            loading = false;
                          });
                        }
                      }
                  ),
                ),
                Builder(builder: (context){
                  return Container(
                      width: MediaQuery
                          .of(context)
                          .size
                          .width - 50.0,
                      child: Text(text)
                  );
                })
              ],
            ),
          ),
        ),
      ),
    );
  }
}
4.效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值