flutter顶部小部件
In this article we’re going to investigate how we can use callback-style events to communicate between widgets with Flutter.
在本文中,我们将研究如何使用Flutter在回调控件之间使用回调风格的事件进行通信。
Why is this important? It allows us to separate our widgets into small, testable units that can be adaptable to their context.
为什么这很重要? 它使我们可以将小部件分成可测试的小单元,以适应其上下文。
创建一个新的Flutter项目 (Creating a New Flutter Project)
As always, we’ll start off by setting up a new project:
与往常一样,我们将从建立一个新项目开始:
# New Flutter project
$ flutter create widget_communication
# Open this up inside of VS Code
$ cd widget_communication && code .
We can now open this up in the iOS or Android simulator from within VS Code.
现在,我们可以在VS Code中的iOS或Android模拟器中打开它。
算这个! (Count This!)
The first method we’re going to use is simply passing data down to the child as a property. Let’s update main.dart
to contain a reference to our CounterPage
that we’ll create in a second:
我们将要使用的第一种方法只是将数据作为属性传递给子级。 让我们更新main.dart
以包含对我们的CounterPage
的引用,该引用将在第二秒创建:
import 'package:flutter/material.dart';
impor