flutter最简单的天气预报

flutter最简单的天气预报

项目结构

在这里插入图片描述

介绍

在这里插入图片描述

预览图

  1. 首页

在这里插入图片描述

  1. 左右滑动的导航条

在这里插入图片描述

  1. 下拉刷新

在这里插入图片描述

  1. 未来七天的天气

在这里插入图片描述

  1. 未来七天天气的刷新

在这里插入图片描述

代码

代码链接:flutter最简单的天气预报

逻辑

  1. 封装天气
  static const forecast = {
    0: Icons.wb_sunny_outlined,
    1: Icons.cloud_queue,
    2: Icons.ac_unit,
    3: Icons.flash_on,
    4: Icons.waves,
  };

在这里插入图片描述

  1. 封装日期
class listview extends State {
  SecondScreen sec = new SecondScreen();
  static const date = {
    0: "今天",
    1: "星期二",
    2: "星期三",
    3: "星期四",
    4: "星期五",
    5: "星期六",
    6: "星期日",
  };

在这里插入图片描述

  1. 封装随机温度
randomBit(int len) {
    String scopeF = '1'; //首位
    String scopeC = '0123456789'; //中间
    String scopeD = '01234';
    String result = '';
    if (len == 2) {
      for (int i = 0; i < len; i++) {
        if (i == 0) {
          result = scopeF[Random().nextInt(scopeF.length)];
        } else {
          result = result + scopeC[Random().nextInt(scopeC.length)];
        }
      }
      return result;
    } else {
      for (int i = 0; i < len; i++)
        result = result + scopeD[Random().nextInt(scopeD.length)];
      return result;
    }
  }

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flutter 是一个跨平台的移动应用开发框架,可以使用 Dart 语言进行开发。要实现天气预报功能,你可以按照以下步骤进行操作: 1. 导入相关依赖:在 pubspec.yaml 文件中添加 http 和 json_serializable 依赖。http 用于发送网络请求,json_serializable 用于序列化和反序列化 JSON 数据。 ```yaml dependencies: flutter: sdk: flutter http: ^0.13.3 json_serializable: ^4.4.0 ``` 2. 创建数据模型:根据天气 API 返回的 JSON 数据,创建相应的数据模型。例如,可以创建 Weather 类来表示天气信息。 ```dart import 'package:json_annotation/json_annotation.dart'; part 'weather.g.dart'; @JsonSerializable() class Weather { final String city; final double temperature; final String description; Weather({required this.city, required this.temperature, required this.description}); factory Weather.fromJson(Map<String, dynamic> json) => _$WeatherFromJson(json); Map<String, dynamic> toJson() => _$WeatherToJson(this); } ``` 3. 生成序列化代码:运行以下命令生成序列化代码: ```bash flutter pub run build_runner build ``` 4. 创建网络请求服务:可以创建一个 WeatherService 类,封装获取天气数据的网络请求。 ```dart import 'package:http/http.dart' as http; class WeatherService { static const String apiKey = 'YOUR_API_KEY'; static Future<Weather> fetchWeather(String city) async { final url = Uri.parse('https://api.weatherapi.com/v1/current.json?key=$apiKey&q=$city'); final response = await http.get(url); if (response.statusCode == 200) { final json = jsonDecode(response.body); return Weather.fromJson(json['current']); } else { throw Exception('Failed to fetch weather'); } } } ``` 5. 创建天气预报界面:使用 Flutter 的 Widget 构建界面,可以显示天气信息。 ```dart import 'package:flutter/material.dart'; class WeatherPage extends StatefulWidget { final String city; const WeatherPage({required this.city}); @override _WeatherPageState createState() => _WeatherPageState(); } class _WeatherPageState extends State<WeatherPage> { Weather? _weather; @override void initState() { super.initState(); _fetchWeather(); } void _fetchWeather() async { try { final weather = await WeatherService.fetchWeather(widget.city); setState(() { _weather = weather; }); } catch (e) { print('Failed to fetch weather: $e'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Weather Forecast'), ), body: Center( child: _weather != null ? Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('City: ${_weather!.city}'), Text('Temperature: ${_weather!.temperature}'), Text('Description: ${_weather!.description}'), ], ) : CircularProgressIndicator(), ), ); } } ``` 以上是一个简单的示例,通过 WeatherService 类发送网络请求获取天气数据,并在 WeatherPage 中显示天气信息。你需要替换 apiKey 为你自己的天气 API 密钥,并根据具体的天气 API 接口文档进行调整。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lazy_Goat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值