关于建设学校网站的报告,专业seo网络推广,什么是网站名,网站电脑端和手机端## #x1f324;️ 引言在本篇文章中#xff0c;我们将使用 **Flutter** 构建一个跨平台的 **天气预报应用**。它将通过调用免费的天气 API 获取实时天气数据#xff0c;并以美观的卡片形式展示给用户。我们将涵盖以下关键技术点#xff1a;- 网络请求#xff08;http 包️ 引言在本篇文章中我们将使用 **Flutter** 构建一个跨平台的 **天气预报应用**。它将通过调用免费的天气 API 获取实时天气数据并以美观的卡片形式展示给用户。我们将涵盖以下关键技术点- 网络请求http 包- JSON 数据解析- 异步编程Future 与 async/await- 状态管理基础- 自定义 UI 组件最终效果如下*图运行在 Android 上的 Flutter 天气应用*---## 准备工作### 1. 创建项目bashflutter create weather_appcd weather_app### 2. 添加依赖编辑 pubspec.yaml 文件添加网络请求库yamldependencies:flutter:sdk: flutterhttp: ^0.16.0 # 用于发送HTTP请求然后运行bashflutter pub get### 3. 获取天气 API 密钥我们使用 [OpenWeatherMap](https://openweathermap.org/api) 的免费 API。注册后获取你的 API Key例如your_api_key_here 提示不要将密钥硬编码在代码中此处为教学简化处理。---## ️ 定义城市与天气模型创建文件 lib/models/weather.dartdartclass Weather {final String city;final double temperature;final String description;final String iconCode;Weather({required this.city,required this.temperature,required this.description,required this.iconCode,});// 工厂构造函数从 JSON 创建对象factory Weather.fromJson(MapString, dynamic json) {return Weather(city: json[name],temperature: json[main][temp].toDouble(),description: json[weather][0][description],iconCode: json[weather][0][icon],);}}---## 调用天气 API创建 lib/services/weather_service.dartdartimport package:http/http.dart as http;import dart:convert;import ../models/weather.dart;class WeatherService {static const String baseUrl https://api.openweathermap.org/data/2.5/weather;static const String apiKey your_api_key_here; // 替换为你自己的KeyFutureWeather getWeather(String cityName) async {final response await http.get(Uri.parse($baseUrl?q$cityNameappid$apiKeyunitsmetriclangzh_cn));if (response.statusCode 200) {final data json.decode(response.body);return Weather.fromJson(data);} else {throw Exception(Failed to load weather);}}}---## 构建主页面 UI替换 lib/main.dart 内容dartimport package:flutter/material.dart;import package:weather_app/models/weather.dart;import package:weather_app/services/weather_service.dart;void main() {runApp(const MyApp());}class MyApp extends StatelessWidget {const MyApp({super.key});overrideWidget build(BuildContext context) {return MaterialApp(title: Flutter 天气预报,theme: ThemeData(primarySwatch: Colors.blue,useMaterial3: true,),home: const WeatherPage(),debugShowCheckedModeBanner: false,);}}class WeatherPage extends StatefulWidget {const WeatherPage({super.key});overrideStateWeatherPage createState() _WeatherPageState();}class _WeatherPageState extends StateWeatherPage {final TextEditingController _controller TextEditingController();Weather? _weather;bool _loading false;final WeatherService _service WeatherService();overridevoid dispose() {_controller.dispose();super.dispose();}Futurevoid _fetchWeather() async {if (_controller.text.isEmpty) return;setState(() {_loading true;});try {final weather await _service.getWeather(_controller.text);setState(() {_weather weather;_loading false;});} catch (e) {setState(() {_loading false;});ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(错误: $e)),);}}overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text(️ 天气预报),centerTitle: true,),body: Padding(padding: const EdgeInsets.all(16.0),child: Column(children: [Row(children: [Expanded(child: TextField(controller: _controller,decoration: const InputDecoration(labelText: 输入城市名称,border: OutlineInputBorder(),),onSubmitted: (_) _fetchWeather(),),),const SizedBox(width: 8),IconButton(icon: const Icon(Icons.search),onPressed: _fetchWeather,),],),const SizedBox(height: 20),// 加载状态if (_loading)const CircularProgressIndicator()else if (_weather ! null)Expanded(child: Card(elevation: 4,child: Padding(padding: const EdgeInsets.all(20.0),child: Column(children: [Text(${_weather!.city},style: const TextStyle(fontSize: 28,fontWeight: FontWeight.bold,),),const SizedBox(height: 10),Image.network(https://openweathermap.org/img/wn/${_weather!.iconCode}2x.png,width: 100,),Text(${_weather!.temperature.toStringAsFixed(1)}°C,style: const TextStyle(fontSize: 40,color: Colors.orange,),),Text(${_weather!.description}.toUpperCase(),style: const TextStyle(fontSize: 16,color: Colors.grey,),),],),),),)elseconst Expanded(child: Center(child: Text(请输入城市名称查询天气,style: TextStyle(fontSize: 18, color: Colors.grey),),),),],),),);}}---## ️ 运行效果展示### 启动应用bashflutter run### 查询“北京”天气*图成功显示北京当前天气*### 错误提示示例如果城市不存在会弹出提示---## 技术亮点解析| 特性 | 实现方式 ||------|----------|| **网络请求** | 使用 http.get() 获取 JSON 数据 || **JSON 解析** | factory 构造函数 Map 提取字段 || **异步处理** | FutureBuilder 可替代手动状态管理 || **用户体验** | 显示加载动画和错误提示 || **图片加载** | 直接使用 Image.network 加载图标 |---## ️ 安全建议进阶虽然本例直接写入了 API Key但在生产环境中应- 使用环境变量或配置文件- 通过后端代理请求避免密钥泄露- 使用 dotenv 包管理敏感信息yaml# 添加到 pubspec.yamldev_dependencies:dotenv: ^5.0.2---## 扩展功能建议你可以继续优化这个应用✅ 添加定位功能获取当前位置天气✅ 支持多城市收藏列表✅ 展示未来几天预报调用 forecast API✅ 深色模式切换✅ 下拉刷新---## 打包发布构建 APK 发布到手机bashflutter build apk --release生成 IPA需 macOSbashflutter build ipa---## ✅ 总结通过这个项目你学会了如何- 使用 Flutter 构建真实世界的应用- 调用外部 RESTful API- 解析 JSON 并更新 UI- 处理加载、成功、错误三种状态- 设计简洁美观的界面Flutter 让这一切变得简单而高效---