NotifyListeners不重新构建窗口小部件

时间:2020-05-26 13:17:30

标签: android flutter flutter-layout flutter-provider

我正在构建一个应用程序,该应用程序的屏幕上包含许多文本字段,您可以在其中移动和自定义。我想通过单击单独的有状态小部件中的按钮来更改文本字段的字体大小。因此,我尝试通过声明更改通知程序类来实现提供程序包,如下所示:

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没有传递给状态类,所以我该如何克服呢?

0 个答案:

没有答案