2.get post请求

2019-12-23  本文已影响0人  冰点雨
b4093863d31630533a3522174d4dd74.png
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class HomePage extends StatefulWidget {
  _HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage>{

  TextEditingController typeController = TextEditingController();
  String showText = '欢迎你来到美好人间';


  @override
  Widget build(BuildContext context){
    return Container(

      child: Scaffold(
        appBar: AppBar(title: Text('9999'),),
        body: SingleChildScrollView(
          child: Container(
            height: 1000,
            child: Column(
              children: <Widget>[
                TextField(
                  controller: typeController,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(10.0),
                    labelText: '类型',
                    helperText: '请输入你喜欢的类型',
                  ),
                  autofocus: false,
                ),

                RaisedButton(
                  onPressed: _choiceAction,
                  child: Text('选择完成'),
                ),

                Text(
                  showText,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
              ],
            ),
          ),
        )
      ),

    );
  }

  void _choiceAction(){

    if (typeController.text.toString() == '') {
      showDialog(
          context: context,
          builder: (context)=>AlertDialog(title:Text('美女类型不能为空'))
      );
    }else{
      getHttp(typeController.text.toString()).then((val){
      setState(() {
      showText=val['data']['name'].toString();
      });

      });


    }

  }








  Future getHttp(String TypeText)async{
    try{
      Response response;
      var data={'name':TypeText};
      response = await Dio().post(
          "https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",
          queryParameters:data
      );
      return response.data;
    }catch(e){
      return print(e);
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读