Flutter Provider状态管理---八种提供者使用分析

前言

在我们上一篇文章中对Provider进行了介绍以及类结构的说明,最后还写了一个简单的示例,通过上一章节我们对Provider有了一个基本的了解,这一章节我们来说说Provider的8种提供者以及他们的使用区别。

Provider

Provider是最基本的Provider组件,可以使用它为组件树中的任何位置提供值,但是当该值更改的时候,它并不会更新UI,下面我们给出一个示例

第一步:创建模型
class UserModel {

  String name = "Jimi";

  void changeName() {
    name = "hello";
  }
}
第二步:应用程序入口设置
return Provider<UserModel>(
  create: (_) => UserModel(),
  child: MaterialApp(
    debugShowCheckedModeBanner: false,
    home: ProviderExample(),
  ),
);
第三步:使用共享数据

关于Consumer后面将消费者在提及,我们这里只需要知道有两个消费者,第一个用于展示模型的数据,第二个用于改变模型的数据。

  • 第一个Comsumer是用于读取模型的数据name
  • 第二个Consumer用于改变模型的数据name
import 'package:flutter/material.dart';
import 'package:flutter_provider_example/provider_example/user_model.dart';
import 'package:provider/provider.dart';

class ProviderExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ProviderExample"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Consumer<UserModel>(
              builder: (_, userModel, child) {
                return Text(userModel.name,
                    style: TextStyle(
                        color: Colors.red,
                        fontSize: 30
                    )
                );
              },
            ),
            Consumer<UserModel>(
              builder: (_, userModel, child) {
                return Padding(
                  padding: EdgeInsets.all(20),
                  child: ElevatedButton(
                    onPressed: (){
                      userModel.changeName();
                    },
                    child: Text("改变值"),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

继续阅读Flutter Provider状态管理---八种提供者使用分析

WireGuard 教程:WireGuard 的工作原理

本文翻译自:https://github.com/pirate/wireguard-docs

WireGuard 是由 Jason Donenfeld 等人用 C 语言编写的一个开源 VPN 协议,被视为下一代 VPN 协议,旨在解决许多困扰 IPSec/IKEv2OpenVPN 或 L2TP 等其他 VPN 协议的问题。它与 Tinc 和 MeshBird 等现代 VPN 产品有一些相似之处,即加密技术先进、配置简单。从 2020 年 1 月开始,它已经并入了 Linux 内核的 5.6 版本,这意味着大多数 Linux 发行版的用户将拥有一个开箱即用的 WireGuard。

无论你是想破墙而出,还是想在服务器之间组网,WireGuard 都不会让你失望,它就是组网的『乐高积木』,就像 ZFS 是构建文件系统的『乐高积木』一样。

继续阅读WireGuard 教程:WireGuard 的工作原理

满血复活 手把手教你更换iPad 2电池

查阅了一些拆解教程,发现难度不小。虽然有拆解平板的经验,但实际拆解这部iPad 2遇到的困难超过心理预期。

难度主要集中在三个方面:

1、虽然iPad 2代表了当年最高的工艺、工业设计。但彼时的屏幕,触控屏和显示屏并不是一个总成。也就是「外屏」、「内屏」是分体的。且外屏的排线压在内屏下面。

2、彼时「易拉胶」还没有广泛应用到数码产品上,iPad 2内部都是双面胶。

3、无尘环境。

结合步骤,下文详述。

继续阅读满血复活 手把手教你更换iPad 2电池

Flutter 状态管理框架 Provider 和 Get 分析

状态管理一直是 Flutter 开发中一个火热的话题。谈到状态管理框架,社区也有诸如有以 GetProviderRiverpod 为代表的多种方案,它们有各自的优缺点。面对这么多的选择,你可能会想:「我需要使用状态管理么?哪种框架更适合我?」本文将从作者的实际开发经验出发,分析状态管理解决的问题以及思路,希望能帮助你做出选择。

为什么需要状态管理?

首先,为什么需要状态管理?根据笔者的经验,这是因为 Flutter 基于 声明式 构建 UI ,使用状态管理的目的之一就是解决「声明式」开发带来的问题。

「声明式」开发是一种区别于传原生的方式,所以我们没有在原生开发中听到过状态管理,那如何理解「声明式」开发呢?

继续阅读Flutter 状态管理框架 Provider 和 Get 分析

Flutter Riverpod 全面深入解析,为什么官方推荐它?

随着 Flutter 的发展,这些年 Flutter 上的状态管理框架如“雨后春笋”般层出不穷,而近一年以来最受官方推荐的状态管理框架无疑就是 Riverpod ,甚至已经超过了 Provider ,事实上 Riverpod 官方也称自己为 “Provider,但与众不同”。

Provider 本身用它自己的话来说是 “InheritedWidget 的封装,但更简单且复用能力更强。” ,而 Riverpod 就是在 Provider 的基础上重构了新的可能。

关于过去一年状态管理框架的对比可以看 《2021 年的 Flutter 状态管理:如何选择?》本文主要是带你解剖 RiverPod 的内部是如何实现,理解它的工作原理,以及如何做到比 Provider 更少的模板和不依赖 BuildContext

继续阅读Flutter Riverpod 全面深入解析,为什么官方推荐它?

macOS Sonoma(14.2.1)通过Docker编译Android 12.1源码过程总结(MacBook Pro 2023-Apple M2 Pro)

前置条件


根据 Google 官方文档,2021年6月22日之后的Android系统版本不支持在macOS系统上构建,我们在 Applic SiliconmacOS 系统是不能直接成功构建后续版本的,但是之前的版本可以在修改编译配置后成功编译,只是是否能正常运行存疑

另外,我们需要安装 Rosetta 2 支持运行部分 x86_64 应用。注意  Rosetta 2 只支持 64 位应用,不支持 32 位应用。 参考 Does Rosetta 2 support 32-bit Intel apps?

继续阅读macOS Sonoma(14.2.1)通过Docker编译Android 12.1源码过程总结(MacBook Pro 2023-Apple M2 Pro)

M1 CPU 那么多的核,macOS 是怎样管理的?

1984 年 1 月,Apple 开始设计、开发和销售个人电脑系列产品 Macintosh。在这近 40 年时间里,Apple 浓墨重彩地书写了许多科技史上的里程碑,其中就包括了三个非常重要的时间点——1994 年从摩托罗拉 68000 架构迁移至 PowerPC 平台、 2005 年从 PowerPC 平台迁移至英特尔 x86 平台、2020 年从英特尔 x86 平台迁移至 Apple Silicon。

2020 年 11 月 11 日 Apple 在加州 Cupertino 正式发布了 M1 芯片,不仅是 Apple 自己首款基于 ARM 架构的用于个人电脑的自研处理器,而且其强大的性能也让当时苦「牙膏厂」久已的 Geeker 们也感到异常亢奋。

不过,看得见的风光总是与看不到的努力分不开的。作为专为 Mac 设计、优化的芯片,系统到底是怎么将程序调度在 M 系列处理器上的。

继续阅读M1 CPU 那么多的核,macOS 是怎样管理的?

Box64 running on M1 with Asahi

The Asahi project has now released a first alpha version, so I tried that on a MacBookPro. Asahi is a full linux distribution that can be installed on mac with an M1 processor, it installs alongside your current macOS. It’s an alpha release so many things are missing for now, including 3D hardware acceleration, but on the other hand, for an alpha release, many things are already working, and working fine!

After a (quite) long installation process, I got linux running on that mac. Note that I add to restart the installation process, as the mac went to sleep mode during “root expension” and that actually froze the installation. I needed to break, remove all the create partitions (a risky process) to be able to start again.

Now, the M1 processor is very fast ARM64 processor, but it only supports 64bits operations. No ARM32 there, so no box86. Also, the Asahi readme mentions that the Pagesize is 16K, instead of the standard 4K. That breaks a few things on the way. Fortunatly, I had already encountered a system with 16K pagesize (Loongarch64 systems), so there is some supporting code on box64 already for it. And after enabling it (plus some compilation options), box64, with dynarec, was running!

To try things, I wanted to install a simple linux game. Remember that there is no Hardware acceleration yet, so I choose WorldOfGoo, with its simple graphics (it’s still an OpenGL game). Launching the install didn’t work, as by default, the setup tries to run x86 code (32bits, so needing box86 not available here). To work around that, I created a simple uname script that spoofs the setup into thinking this is an x86_64 system:

the file simply contains

#!/bin/bash

if [ "$1" == "-m" ];then
 echo x86_64
 exit 0
fi

/bin/uname "$@"

And I created an x86_64 folder in my home directory to put some pc stuffs (and that uname script)

Then I launched the setup as PATH=~/x86_64:$PATH ./WorldOfGoo.Linux.153.sh et voilà, the setup launched… But as text only… Ok, gtk2 libs are not installed. So sudo pacman -S gtk2 and try again…

继续阅读Box64 running on M1 with Asahi