android - NotifyListeners 不会在颤动中重建小部件
问题描述
我正在构建一个应用程序,它的屏幕上有很多文本字段,您可以四处移动和自定义。我想通过单击位于单独的有状态小部件中的按钮来更改文本字段的字体大小。所以我尝试通过声明一个更改通知类来实现提供程序包,如下所示:
class ChangeNotifierList with ChangeNotifier {
List<Widget> memeWidgets= new List();
void add(Widget widget){
memeWidgets.add(widget);
notifyListeners();
}
void remove(Widget widget){
memeWidgets.remove(widget);
notifyListeners();
}
void increaseFontSize(StateTextField widget){
widget.increaseFontSize();
notifyListeners();
}
}
并单击一个按钮,我更改了文本字段的字体大小,例如:
onTap: () {
setState(() {
Provider.of<ChangeNotifierList>(context,listen: false)
.increaseFontSize(widget.textField);
//widget.textField.increaseFontSize();
});
}
我的小部件显示在堆栈中,堆栈定义如下:
Container(
height: MediaQuery.of(context).size.height * 0.8,
child: Consumer<ChangeNotifierList>(
builder: (context, value, child) {
return Stack(children: value.memeWidgets);
},
),
)
我的 textField 小部件如下:
class StateTextField extends StatefulWidget {
GlobalKey key = GlobalKey();
final FocusNode focusNode = FocusNode();
final Function(bool, Widget) callback;
final String fontFamily = FontFamily.Arial.toString().split('.')[1];
double fontSize = 18;
final Function(bool) selected;
final bool highlighted = false;
bool hasFocus() {
return focusNode.hasFocus;
}
increaseFontSize() {
fontSize += 2;
}
decreasefontSize() {
if (fontSize > 0) fontSize -= 2;
}
// increaseFontSize() {
// fontSize += 2;
// }
// decreasefontSize() {
// if (fontSize > 0) fontSize -= 2;
// }
StateTextField(
{@required this.callback,
@required this.selected,
@required this.fontSize});
@override
_StateTextFieldState createState() => _StateTextFieldState();
}
我知道问题是因为更改后的 fontSize 没有传递给状态类,那么我该如何克服呢?
解决方案
推荐阅读
- rest - Coverity 的 REST API 是否有办法获取每个项目和流的问题?
- wordpress - 如何更改 woocommerce 中的 ORDER ID 生成器?
- java - 将 PDFBox 文档写入文件时出现 OutOfMemoryException / 是否可以即时写入?
- swift - Swift 4:可解码的空容器
- r - 哪个用户在 OpenCPU 服务器端运行 R?
- c - 为什么`execv`不能使用从char**到char* const*的隐式转换?
- vb.net - 使用 Word 互操作的问题 - 在模板上插入图像
- boost - 使用自定义属性提升 BOOST_LOG_SEV
- openshift - 从 OpenShift CLI 查看服务主机名
- javascript - new Websocket() 在 Firefox Quantum 中返回错误的对象