有没有一种方法可以在不使用小部件的情况下访问PageState类中的页面属性

时间:2019-11-16 14:57:54

标签: flutter

我想使用widget.customerInfo.firstName作为TextFormField的初始文本,但是我不能使用控制器:TextEditingController(text: widget.customerInfo.firstName),因为此类中有很多TextFormField,我需要才能按名称呼叫每个TextEditingController

所以另一种选择是在Widget构建之外初始化最终的firstnameController = TextEditingController();,但是问题是我将无法访问widget.customerInfo.firstName,所以我还能如何访问`customerInfo.firstName。 ?

这是我的代码:

import 'package:flutter/material.dart';
import './customerInfo.dart';
import './loginPage.dart';

class UserInfoPage2 extends StatefulWidget {
  UserInfoPage2({Key key, this.title, this.customerInfo, this.token})
      : super(key: key);
  final String title;
  final CustomerInfo customerInfo;
  final String token;

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

class _UserInfoPageState extends State<UserInfoPage2> {  

  void navLogin() {
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => LoginPage()));
  }

  final _formKey = GlobalKey<FormState>();

  final firstnameController = TextEditingController();
  final _lastnameController = TextEditingController();
  final _emailController = TextEditingController();
  final _phoneNumController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Profile Page'),
      ),
      body: Center(
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              TextFormField(
                // initialValue: widget.customerInfo.firstName,
                readOnly: false,
                decoration: const InputDecoration(
                  labelText: 'First Name *',
                ),
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter your first name';
                  }
                  return null;
                },
                controller: TextEditingController(text: widget.customerInfo.firstName),
              ),
              TextFormField(
                // initialValue: widget.customerInfo.lastName,
                readOnly: true,
                decoration: const InputDecoration(
                  labelText: 'Last Name *',
                ),
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter your last name';
                  }
                  return null;
                },
                controller: _lastnameController,
              ),
              TextFormField(
                // initialValue: widget.customerInfo.email,
                readOnly: true,
                decoration: const InputDecoration(
                  labelText: 'Email *',
                ),
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter your email';
                  }
                  return null;
                },
                controller: _emailController,
              ),
              TextFormField(
                // initialValue: widget.customerInfo.phoneNum,
                readOnly: true,
                decoration: const InputDecoration(
                  labelText: 'Phone Number *',
                ),
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter your phone number';
                  }
                  return null;
                },
                controller: _phoneNumController,
                keyboardType: TextInputType.number,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您可以在initState函数中完成此操作,

TextEditingController firstnameController ;

initState(){   
         firstNameController = 
   TextEditingController(text:widget.customerInfo.firstName);
  }

此函数在构建函数之前被调用,因此您可以初始化任何变量或进行一些工作,例如在构建小部件之前调用函数