【问题标题】:Change textSize of textfield from another widget Flutter从另一个小部件 Flutter 更改文本字段的 textSize
【发布时间】:2020-09-12 22:18:08
【问题描述】:

我需要通过单击另一个小部件中的图标来更改 textField 的 fontSize。所以我在这里有我的自定义文本字段小部件。

class StateTextField extends StatefulWidget {
  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;
  }

  StateTextField({@required this.callback,@required this.selected});
  @override
  _StateTextFieldState createState() => _StateTextFieldState();
}

在第二个小部件中,我使用了函数 increaseFontSize 和 reductionFontSize 来改变大小

 onTap: () {
                setState(() {
                  print(widget.textField.fontSize);
                  widget.textField.increaseFontSize();

                  print(widget.textField.fontSize);
                });
              }

点击按钮后尺寸会增加,但不会反映出来。我意识到这是因为 setState 不会改变 textField 的状态。那我应该采用什么方法呢?

【问题讨论】:

  • 你应该有一个全局状态并且可以使用任何状态管理技术来完成,我建议你看看provider包。

标签: android flutter flutter-layout


【解决方案1】:

创建一个变量文件并将其导入您的 main.dart 或任何您需要的地方。

double fontSize = 12;

变量.dart

Column(
  children: <Widget>[
    TextField(
      style: TextStyle(
        fontSize: fontSize
      )
    ),
    Container(
      width: double.infinity,
      height: 70,
      child: RaisedButton(
        onPressed: (){
          setState((){
            fontSize = fontSize +1; //or fontSize+=1;
          });
        },
        child: Center(
          child: Text("+")
        )
      )
    ),
    Container(
      width: double.infinity,
      height: 70,
      child: RaisedButton(
        onPressed: (){
          setState((){
            fontSize = fontSize -1; //or fontSize-=1;
          });
        },
        child: Center(
          child: Text("-")
        )
      )
    )
  ]
)

main.dart

【讨论】:

  • 这会起作用,但有一个全局变量,如here 所示,这不是一个好习惯。
  • 那么他可以将其创建为局部变量,它也可以工作,但只能在类内部
  • 是的,这就是我推荐使用状态管理技术的原因。
  • 无论如何我已经阅读了您发布的链接,虽然它很有帮助,但我认为它并不总是坏事。您会看到在某些情况下,性能和内存需要全局变量
  • 问题是我将根据用户的选择添加动态数量的文本字段。所以创建一个全局变量有点问题,因为我需要多个这样的变量。
【解决方案2】:

有一种方法可以以某种方式帮助您。

第 1 步: 不要在 StatefulWidget 中使用增减方法

第 2 步:将值存储在变量中并在同一个小部件本身中进行更改

class StateTextField extends StatefulWidget {
  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;
  }

  StateTextField({@required this.callback,@required this.selected});
  @override
  _StateTextFieldState createState() => _StateTextFieldState();
}

StateTextFieldState extends State<StateTextField>{
   double _fontSize;

   @override
   void initState(){
      super.initState();

      // setting the value from the widget in initialization of the widget to the local variable which will be used to do the increase-decrease operation
      _fontSize = widget.fontSize;
   }

   void increaseFontSize() {
    setState(() => _fontSize += 2);
   }

   void decreasefontSize() {
     if (_fontSize > 0){
       setState(() => _fontSize -= 2);
     }
   }

   //in your widget method, you can perform your operation now in `onTap`
   onTap: () {
     print(_fontSize);
     //call your increase method here to increase
     _increaseFontSize()
  }
}

如果这在某种程度上对您有帮助,请告诉我。谢谢:)

【讨论】:

  • 无法在小部件类中添加 setState。 setState 只能在 State 类中调用
  • @AYUSHSINGH 我已经更新了我的代码,请看看是否有帮助。
  • 另一个小部件位于不同的有状态小部件中。我将如何在该小部件中访问此功能?
  • 然后向我们展示完整的代码@AYUSHSINGH。这么多代码没有帮助。谢谢
猜你喜欢
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2022-01-16
  • 2018-08-30
  • 2019-12-17
相关资源
最近更新 更多