flutter_qweather 0.0.2 copy "flutter_qweather: ^0.0.2" to clipboard
flutter_qweather: ^0.0.2 copied to clipboard

outdated

A qweather Flutter plugin.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_qweather/constants.dart';
import 'package:flutter_qweather/flutter_qweather.dart';

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

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  /// _location
  /// LocationID 或者 经纬度;
  /// LocationID 可通过geo 接口查询 或 查看https://github.com/qwd/LocationList
  String _location = "116.41,39.92";
  TextEditingController _controller = TextEditingController();
  WeatherNowResp _weatherNowResp;

  @override
  void initState() {
    _controller.text = _location;
    super.initState();
    initPlatformState();
    initQweather();
  }

  // 初始化 Qweather
  Future<void> initQweather() async {
    QweatherConfig config = QweatherConfig(
        publicIdForAndroid: 'HE2104172001541286',
        keyForAndroid: '84538637d3a94c5a91ad5eb82ff6e595',
        publicIdForIos: 'HE2104182135421206',
        keyForIos: 'aead742b44ce480fbf88cb607eec5933',
        biz: false,
        debug: false);
    await FlutterQweather.instance.init(config);
    queryWeatherNow();
    FlutterQweather.instance.getWeatherMinuteLy(_location);
  }

  // 查询实时天气
  Future<void> queryWeatherNow() async {
    setState(() => _weatherNowResp = null);
    // await Qweather.instance.getWeatherNow("101010100");
    _weatherNowResp = await FlutterQweather.instance.getWeatherNow(_location);
    setState(() {});
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await FlutterQweather.instance.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    if (!mounted) return;
    setState(() => _platformVersion = platformVersion);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('QWeather example app')),
        body: Column(
          children: [
            Padding(
              padding: EdgeInsets.all(20),
              child: IntrinsicHeight(
                child: Row(
                  children: [
                    Expanded(
                      child: TextField(
                        controller: _controller,
                        onChanged: (v) => _location = v,
                        decoration: InputDecoration(
                          hintText: "请输入LocationID 或者 经纬度",
                        ),
                      ),
                    ),
                    ElevatedButton(
                      child: Text("查询天气"),
                      onPressed:
                          _weatherNowResp == null || _location.trim().isEmpty
                              ? null
                              : queryWeatherNow,
                    )
                  ],
                ),
              ),
            ),
            Expanded(
                child: _weatherNowResp == null
                    ? Center(child: Text("loading..."))
                    : _weatherNowWidget),
            Container(
              padding: EdgeInsets.all(64),
              child: Text('Running on: $_platformVersion\n'),
            )
          ],
        ),
      ),
    );
  }

  Widget get _weatherNowWidget {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
      alignment: Alignment.center,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
              "原始数据来源:             ${_weatherNowResp.refer.sources.join(",")}"),
          Text(
              "使用许可:                ${_weatherNowResp.refer.license.join(",")}"),
          Divider(),
          Text("接口更新时间:            ${_weatherNowResp.basic.updateTime}"),
          Text("所查询城市的天气预报网页:   ${_weatherNowResp.basic.fxLink}"),
          Divider(),
          Text("实况观测时间:            ${_weatherNowResp.now.obsTime}"),
          Text("体感温度,默认单位:摄氏度: ${_weatherNowResp.now.feelsLike}"),
          Text("温度,默认单位:摄氏度:    ${_weatherNowResp.now.temp}"),
          Text("实况天气状况代码:         ${_weatherNowResp.now.icon}"),
          Text("实况天气状况:             ${_weatherNowResp.now.text}"),
          Text("风向360角度:             ${_weatherNowResp.now.wind360}"),
          Text("风向:                   ${_weatherNowResp.now.windDir}"),
          Text("风力:                   ${_weatherNowResp.now.windScale}"),
          Text("风速,公里/小时:          ${_weatherNowResp.now.windSpeed}"),
          Text("相对湿度:                ${_weatherNowResp.now.humidity}"),
          Text("降水量:                  ${_weatherNowResp.now.precip}"),
          Text("大气压强:                 ${_weatherNowResp.now.pressure}"),
          Text("能见度,默认单位:公里:     ${_weatherNowResp.now.vis}"),
          Text("云量:                    ${_weatherNowResp.now.cloud}"),
          Text("实况云量:                  ${_weatherNowResp.now.dew}"),
        ],
      ),
    );
  }
}