从另一个窗口小部件更改文本字段的textSize

时间:2020-05-26 07:18:18

标签: android flutter flutter-layout

我需要通过单击另一个小部件中的图标来更改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();
}

然后在第二个小部件中,我使用了函数gainFontSize和reduceFontSize来更改大小

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

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

单击按钮时大小会增加,但不会反映出来。我意识到这是因为setState不会更改textField的状态。那我该怎么办?

2 个答案:

答案 0 :(得分:0)

创建一个变量文件并将其导入到main.dart或任何需要的位置。

double fontSize = 12;

variable.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

答案 1 :(得分:0)

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

步骤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()
  }
}

让我知道这是否对您有所帮助。谢谢:)