Flutter天气图标插件qweather_icons的使用

发布于 1周前 作者 yibo5220 来自 Flutter

Flutter天气图标插件qweather_icons的使用

qweather_icons

『 qweather_icons - QWeather Icons! 』

中文 Readme

最新更新: 2023-09-21 17:00:44

📚 简介

这是一个用于展示QWeather图标的插件。

📸 截图

screenshot_1 screenshot_2

或者尝试在线示例应用

📦 如何使用

步骤一:添加最新版本到pubspec.yaml

在项目的pubspec.yaml文件中添加以下依赖:

dependencies:
  qweather_icons: ^1.6.0

步骤二:在任何地方使用它

在Dart代码中使用该图标:

Icon(QweatherIcons.tag_qweather),

步骤三:更多方法

以下是获取不同状态图标的几个方法:

/// 根据标签获取图标
///
/// 如果不存在,则返回默认值 `QWeatherIcons.tag_unknown`
factory QWeatherIcons.getIconWith(String? tag) {
  if (tag == null) return QWeatherIcons.tag_unknown;
  for (QWeatherIcons icons in QWeatherIcons.values) {
    if (icons.tag == tag) return icons;
  }
  return QWeatherIcons.tag_unknown;
}

/// 获取填充图标
///
/// 如果不存在,则返回默认值 `QWeatherIcons.tag_unknown`
factory QWeatherIcons.getFilledIconWith(QWeatherIcons qWeatherIcons) {
  if (qWeatherIcons.tag.endsWith('_fill')) return qWeatherIcons;
  for (QWeatherIcons icons in QWeatherIcons.values) {
    if ('${qWeatherIcons.tag}_fill' == icons.tag) return icons;
  }
  return QWeatherIcons.tag_unknown;
}

/// 获取未填充图标
///
/// 如果不存在,则返回默认值 `QWeatherIcons.tag_unknown`
factory QWeatherIcons.getUnfilledIconWith(QWeatherIcons qWeatherIcons) {
  if (!qWeatherIcons.tag.endsWith('_fill')) return qWeatherIcons;
  for (QWeatherIcons icons in QWeatherIcons.values) {
    if ('${icons.tag}_fill' == qWeatherIcons.tag) return icons;
  }
  return QWeatherIcons.tag_unknown;
}

⏳ 进度

已完成…

📌 注意事项

🧑‍💻 贡献者

贡献者

🔦 声明

该项目采用BSD-3-Clause 许可证。详情请参阅LICENSE


示例代码

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'QWeatherIcons Demo',
      theme: ThemeData.light(
        useMaterial3: true,
      ),
      darkTheme: ThemeData.dark(
        useMaterial3: true,
      ),
      themeMode: ThemeMode.system,
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Icon(QWeatherIcons.tag_qweather_fill.iconData),
            ),
            const Text('QWeatherIcons'),
          ],
        ),
      ),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: MediaQuery.of(context).size.width > 1000 ? 12 : 6,
        ),
        itemCount: QWeatherIcons.values.length,
        itemBuilder: (context, index) => QWeatherCard(
          qWeatherIcons: QWeatherIcons.values[index],
        ),
      ),
    );
  }
}

class QWeatherCard extends StatelessWidget {
  const QWeatherCard({
    super.key,
    required this.qWeatherIcons,
  });

  final QWeatherIcons qWeatherIcons;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Center(
            child: Text(qWeatherIcons.tag),
          ),
          content: SizedBox(
            width: MediaQuery.of(context).size.width / 3,
            child: ListView(
              shrinkWrap: true,
              children: [
                ListTile(
                  leading: Icon(qWeatherIcons.iconData),
                  title: const Text('Usage: '),
                  subtitle: Text(
                    '${qWeatherIcons.runtimeType}.${qWeatherIcons.name}',
                  ),
                ),
                ListTile(
                  leading: Icon(
                    QWeatherIcons.getFilledIconWith(qWeatherIcons).iconData,
                  ),
                  title: const Text('Filled Icon: '),
                  subtitle: Text(
                    '${QWeatherIcons.getFilledIconWith(qWeatherIcons)}',
                  ),
                ),
                ListTile(
                  leading: Icon(
                    QWeatherIcons.getUnfilledIconWith(qWeatherIcons).iconData,
                  ),
                  title: const Text('Unfilled Icon: '),
                  subtitle: Text(
                    '${QWeatherIcons.getUnfilledIconWith(qWeatherIcons)}',
                  ),
                )
              ],
            ),
          ),
        ),
      ),
      child: Card(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Expanded(
              flex: 2,
              child: Icon(qWeatherIcons.iconData),
            ),
            Expanded(child: Text(qWeatherIcons.tag))
          ],
        ),
      ),
    );
  }
}

更多关于Flutter天气图标插件qweather_icons的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter天气图标插件qweather_icons的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用qweather_icons插件的示例代码。qweather_icons是一个用于显示天气图标的Flutter插件,基于和风天气(QWeather)提供的图标集。

1. 添加依赖

首先,你需要在pubspec.yaml文件中添加qweather_icons的依赖:

dependencies:
  flutter:
    sdk: flutter
  qweather_icons: ^最新版本号  # 请替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

2. 导入插件

在你的Dart文件中导入qweather_icons

import 'package:qweather_icons/qweather_icons.dart';

3. 使用图标

qweather_icons插件提供了多种天气图标,你可以根据天气状况来显示相应的图标。以下是一个简单的示例,展示如何在Flutter应用中使用这些图标:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'QWeather Icons Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: WeatherIconScreen(),
    );
  }
}

class WeatherIconScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('QWeather Icons Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // 显示晴天天气图标
            Icon(
              QWeatherIcons.day_sunny,
              size: 64,
              color: Colors.blue,
            ),
            SizedBox(height: 20),
            // 显示雨天天气图标
            Icon(
              QWeatherIcons.day_rain,
              size: 64,
              color: Colors.blue,
            ),
            SizedBox(height: 20),
            // 显示雪天天气图标
            Icon(
              QWeatherIcons.day_snow,
              size: 64,
              color: Colors.blue,
            ),
          ],
        ),
      ),
    );
  }
}

4. 运行应用

保存所有文件后,运行你的Flutter应用。你应该会看到一个包含几个天气图标的简单界面。

注意事项

  • QWeatherIcons类中包含了多种天气图标,你可以根据实际需求选择合适的图标。
  • 你可以通过调整Icon组件的sizecolor属性来改变图标的大小和颜色。
  • 确保你已经正确安装了qweather_icons插件,并且其版本与你的Flutter环境兼容。

这个示例展示了如何在Flutter项目中使用qweather_icons插件来显示天气图标。你可以根据实际需求进一步扩展和定制这个示例。

回到顶部