TextFormField值消失了,如何获取textFormField值?

时间:2019-06-06 05:59:52

标签: flutter dart

请回答我的两个问题。

第一个问题。为什么文字值消失了? [FIXED] by @Praneeth 我加了gif please click

我以这种方式使用我的代码,

在我的小部件中,我叫UsernameTextField类。 窗口小部件如下:

  

小部件构建> WillPopScope>支架>表单> ListView>子代>   容器(位于容器下方)我也添加了key: _scaffoldKey,key: formKey,

 Container(
                padding: EdgeInsets.all(16.0),
                margin: EdgeInsets.only(top: 30.0),
                child: UsernameTextField(),
              ),

UsernameTextField()

class UsernameTextField extends StatefulWidget{
  final usernameController = TextEditingController();
  @override
  State<StatefulWidget> createState() {
    return UsernameTextFieldState(usernameController);
  }
}

class UsernameTextFieldState extends State<UsernameTextField>{
  final usernameController;
  UsernameTextFieldState(this.usernameController);

  @override
  Widget build(BuildContext context) {
    return AppTextField(
        decoration: InputDecoration(
          contentPadding: const EdgeInsets.all(20.0),
          labelText: AppTranslations.of(context)
              .text("loginpage_username"),
        ),
        myController: usernameController,
        textInputType: TextInputType.emailAddress
    );
  }
}

AppTextField()类,我为每个TextField小部件都使用了该类

class AppTextField extends StatelessWidget {
  final InputDecoration decoration;
  final myController;
  final TextInputType textInputType;
  AppTextField({
    this.decoration,
    this.myController,
    this.textInputType
  });

  @override
  Widget build(BuildContext context) {
    return TextFormField(
        controller: myController,
        keyboardType: textInputType,
        textAlign: TextAlign.left,
        decoration: decoration
    );}}

第二个问题。如何获取textField值?

在我调用的按钮onPressed()方法中,我调用了validation方法,但结果是null

usernameValidation(){
  String username = UsernameTextField().usernameController.text;
  print(username);
} 

1 个答案:

答案 0 :(得分:1)

首先通过扩展为StatefulWidget而不是StatelessWidget来将UsernameTextField类转换为有状态类。 然后,您可以从<div class="container"> <input type="checkbox" name="check" class="checkOption"> check 1 <input type="checkbox" name="check" class="checkOption"> check 2 <input type="checkbox" name="check" class="checkOption"> check 3 <input type="checkbox" name="check" class="checkOption"> check 4 <!-- Trigger the modal with a button --> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#cbreInvoiceDetailsModal">Open Modal</button> <!--Modal html code--> <div class="modal fade" id="cbreInvoiceDetailsModal" role="dialog"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Full screen view</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <div class="col-md-12 col-xs-12 col-sm-7"> <div class="box_layout" id="show-data"> <div class="image_layout"> </div> </div> </div> </div> <div class="modal-footer"> <button id="closeInvoiceModal" type="button" class="btn btn-sm btn-success waves-effect pull-right" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> <script> $(document).ready(function(){ $("#cbreInvoiceDetailsModal").on('hide.bs.modal', function(){ $('.checkOption').prop('checked', false); }); }); </script>

获得价值

更新

usernameController.text