如何在Flutter中使用控制器获取TextFormField文本的颜色,字体大小等? [英] How to get the TextFormField text color,fontsize, etc using controller in flutter?

查看:98
本文介绍了如何在Flutter中使用控制器获取TextFormField文本的颜色,字体大小等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了自定义textformfield类,并将其用作小部件.我想获取已设置的textformfields颜色,字体大小,对齐方式等.如何使用控制器或通过其他方法获取设置为textformfield的所有属性?

I make custom textformfield class and used it as a widget. I want to get the textformfields color,fontsize,align etc which I have set.How can i get this all properties which i have set to the textformfield using controller or by anything?

我的自定义课程代码:

class CustomTextNIconWidget extends StatefulWidget {

  final Color fontColor;
  final GestureTapCallback onSingleTapWidget;
  final GestureTapCallback onDoubleTapWidget;

  final FontWeight fontWeight;

  final TextEditingController controller;

  final double fontSize;

  CustomTextNIconWidget(
      {Key key,
      @required this.hint,
      this.controller,
      this.fontSize,

      this.fontColor,

      this.fontWeight,
      this.textAlign,
      this.onSingleTapWidget,
      this.onDoubleTapWidget})
      : super(key: key);

  @override
  _CustomTextNIconWidgetState createState() => _CustomTextNIconWidgetState();
}

class _CustomTextNIconWidgetState extends State<CustomTextNIconWidget> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: widget.onSingleTapWidget,
      onDoubleTap: widget.onDoubleTapWidget,
      child: Container(
        child: TextFormField(
          controller: widget.controller,
          style: TextStyle(
              color: widget.fontColor,
              fontSize:
                  widget.fontSize == null ? getFontSize() : widget.fontSize,
              fontWeight: widget.fontWeight == null
                  ? getFontWeight()
                  : widget.fontWeight),
          decoration: InputDecoration(
              hintText: widget.hint,
              hintStyle: TextStyle(color:white)),

        ),
      ),
    );
  }

我曾经用作:

CustomTextNIconWidget(
                    controller: myController,
                    hint: getAddress(),
                    fontColor: Colors.amber,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    onSingleTapWidget: (){
                      print("Text color,fontsize:{HowToGetAboveSetProperties}");
                    },
                  ),




推荐答案

您需要为此使用 GlobalKey .创建 GlobalKey< _CustomTextNIconWidgetState> 如下:

You need to use GlobalKey for this. Create a GlobalKey<_CustomTextNIconWidgetState> as follows:

GlobalKey<_CustomTextNIconWidgetState> _customTextNIconWidgetStateKey = GlobalKey<_CustomTextNIconWidgetState>();

将此键作为自定义窗口小部件的参数提供给:

give this key as parameter to your custom widget as:

CustomTextNIconWidget(
  key: _customTextNIconWidgetStateKey, // <-- key
  controller: myController,
  hint: getAddress(),
  fontColor: Colors.amber,
  fontSize: 18,
  fontWeight: FontWeight.bold,
  onSingleTapWidget: (){
    print("Text color,fontsize:{HowToGetAboveSetProperties}");
  },
),

将小部件推入小部件树后,您可以使用以下命令访问参数:

After the widgets is pushed into the widget tree you can access the parameters with:

_customTextNIconWidgetStateKey.currentState.widget

例如,要访问fontColor,您可以执行以下操作:

For example to access the fontColor you can do this:

Color _fontColor = _customTextNIconWidgetStateKey.currentState.widget.fontColor;

要等待窗口小部件被推入窗口小部件树并对其进行回调,可以使用 WidgetsBinding.instance.addPostFrameCallback((_){})

To wait for the widget to get pushed in the widget tree and get callback for the same, you can use WidgetsBinding.instance.addPostFrameCallback((_){})

在您的初始状态或构建方法中使用它.

use this in your init state or build method.

WidgetsBinding.instance.addPostFrameCallback((_){
    Color _fontColor = _customTextNIconWidgetStateKey.currentState.widget.fontColor;
});

要在声明了 _CustomTextNIconWidgetState 的其他dart文件中使用此密钥,则需要将其公开(通过将其重命名为 CustomTextNIconWidgetState ).

To use this key in different dart file then where your _CustomTextNIconWidgetState is declared, you will need to make it public (by renaming it to CustomTextNIconWidgetState).

进行所有上述更改后,您将获得类似于以下内容的代码:

After you make all the above changes you will be having a code something similar to following:

class MyApp extends StatelessWidget {

  GlobalKey<CustomTextNIconWidgetState> _customTextNIconWidgetStateKey = GlobalKey<CustomTextNIconWidgetState>();

  @override
  Widget build(BuildContext context) {

    WidgetsBinding.instance.addPostFrameCallback((_){
      Color _fontColor = _customTextNIconWidgetStateKey.currentState.widget.fontColor;
    });

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: CustomTextNIconWidget(
            key: _customTextNIconWidgetStateKey, // <-- key
            controller: myController,
            hint: getAddress(),
            fontColor: Colors.amber,
            fontSize: 18,
            fontWeight: FontWeight.bold,
            onSingleTapWidget: (){
              print("Text color,fontsize:{HowToGetAboveSetProperties}");
            },
          ),
        ),
      ),
    );
  }
}

我希望这会有所帮助,如果有任何疑问,请发表评论.如果此答案对您有帮助,请接受并投票.

I hope this helps, in case of any doubts please comment. If this answer helps you then please accept and up-vote.

这篇关于如何在Flutter中使用控制器获取TextFormField文本的颜色,字体大小等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆