2

我有一个以下状态完整的小部件。我需要通过更改两个变量idcollectionName. 通常我会提取一个小部件,但在这种情况下,我正在修改变量 firstName,它不会让我提取小部件。

class IndividualSignupPage1 extends StatefulWidget {
  static final id = 'idIndividualSignupPage1';
  final collectionName = 'Individual';

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

class _IndividualSignupPage1State extends State<IndividualSignupPage1> {
  String firstName;
  DateTime birthDate;
  final firestoreObj = Firestore.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: GeneralAppBar(
        appBar: AppBar(),
      ),
      body: Container(
        child: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[
          TextField(
            onChanged: (value) {
              this.firstName = value;
            },
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Expanded(
                child: Card(
                  child: ListTile(
                    title: Text(
                      this.birthDate == null
                          ? 'Birthdate'
                          : '${this.birthDate.year}-${this.birthDate.month}-${this.birthDate.day}',
                    ),
                    onTap: () {
                      DatePicker.showDatePicker(
                        context,
                        initialDateTime: this.birthDate,
                        onConfirm: (newDate, listOfIndexes) {
                          setState(() {
                            this.birthDate = newDate;
                          });
                        },
                      );
                    },
                  ),
                ),
              ),
            ],
          ),
          WFullWidthButton(
            name: 'Save',
            onPressedFunc: () async {
              // save all info to firestore db
              firestoreObj.collection(widget.collectionName).document('xyz').setData({
                'firstName': this.firstName,
                'birthDate': this.birthDate,
              }, merge: true);
            },
          ),
        ]),
      ),
    );
  }
}

谢谢

4

2 回答 2

4

您可以将参数传递给类 IndividualSignupPage1,然后在其相应的状态类 _IndividualSignupPage1State 中使用它,并使用属性“widget”。喜欢,

// pass the arguments from another class.
class IndividualSignupPage1 extends StatefulWidget {
  final String id;
  final String collectionName;

  IndividualSignupPage1(this.id,this.collectionName);

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

假设您想在其相应的状态类 _IndividualSignupPage1State 中使用 id 和 collectionName ,您可以使用“widget”属性访问它,例如,

appBar: AppBar(title: Text(widget.id)), 
 **OR**
appBar: AppBar(title: Text(widget.collectionName)), 

注意:您只能访问函数/方法中的小部件属性。

于 2020-01-02T07:40:14.067 回答
2

创建 IndividualSignupPage1 构造函数并使用构造函数参数传递数据。

class IndividualSignupPage1 extends StatefulWidget {
  final String id;
  final String collectionName;

  IndividualSignupPage1(this.id,this.collectionName);
于 2020-01-02T07:26:48.493 回答