在子类中更改子类 PartnerInviter 的类成员时,它也会以某种方式更改父类的状态,即 PartnerConnect 。 isInviteOption:true,isInviteSent:false =>父窗口小部件将标志重置为false,false。我正在调用PartnerConnect的构建。我该如何解决?
父部件是:
class PartnerConnect extends StatefulWidget{
bool isInviter = true;
@override
State<StatefulWidget> createState() {
return PartnerConnectState();
}
}
class PartnerConnectState extends State<PartnerConnect>{
final _scaffoldKey = GlobalKey<ScaffoldState>();
double card_elevation = 1;
TextEditingController phoneNumber = TextEditingController();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
key: _scaffoldKey,
body: widget.isInviter ? PartnerInviter(
isInviteOption: false,
isInviteSent: false,
scaffoldKey: _scaffoldKey,
) : PartnerInvitee(
isInvitationAccepted: true,
inviterNumber: '9********0',
scaffoldKey: _scaffoldKey),
appBar: AppBar(
title: Text('Partner Connect'),
backgroundColor: Colors.white,
elevation: 0.0,
),
backgroundColor: Colors.white,
),
);
}
}
子窗口小部件是:
class PartnerInviter extends StatefulWidget{
bool isInviteOption = false;
bool isInviteSent = false;
GlobalKey<ScaffoldState> scaffoldKey;
PartnerInviter({this.isInviteOption, this.isInviteSent, this.scaffoldKey});
@override
State<StatefulWidget> createState() {
return _PartnerInviterState();
}
}
class _PartnerInviterState extends State<PartnerInviter> {
double card_elevation = 1;
TextEditingController phoneNumber = TextEditingController();
@override
Widget build(BuildContext context) {
return inviterConnectBody();
}
Widget inviterConnectBody(){
return Container(
margin: EdgeInsets.all(MediaQuery.of(context).size.width*0.1),
child: ListView(
children: <Widget>[
Visibility(
child: numberInputWidget(),
visible: widget.isInviteOption,
replacement: Container(
child: Visibility(
child: RequestReceivedWidget(phoneNum: phoneNumber.value.text),
visible: widget.isInviteSent,
replacement: Container(
child: RequestRevokeWidget(phoneNum: phoneNumber.value.text),
),
),
),
)
],
),
);
}
Widget RequestRevokeWidget({String phoneNum}){
return Card(
elevation: card_elevation,
color: Colors.deepPurple[50],
child: Container(
height: MediaQuery.of(context).size.height * 0.3,
child: ListView(
children: <Widget>[
ListTile(
title: Text('Partner connected'),
subtitle: Text('Registered number: ${phoneNum}'),
),
Center(
child: Container(
margin: EdgeInsets.only(left: MediaQuery.of(context).size.width * 0.04, right: MediaQuery.of(context).size.width * 0.04),
child: RaisedButton(
color: Colors.deepPurple,
child: Padding(
padding: EdgeInsets.only(bottom: 5),
child: Text('Revoke access', style: TextStyle(color: Colors.white),),),
onPressed: (){
setState(() {
widget.isInviteOption = true;
SnackBar snackBar = SnackBar(content: Text('Partness access successfully revoked'),);
widget.scaffoldKey.currentState.showSnackBar(snackBar);
});
},
),
)),
],
),
),);
}
Widget RequestReceivedWidget({String phoneNum}){
return Card(
elevation: card_elevation,
color: Colors.deepPurple[50],
child: Container(
height: MediaQuery.of(context).size.height * 0.3,
child: ListTile(
title: Text('Waiting for partner to accept invite'),
subtitle: Text('Registered number: ${phoneNum}'),
),
),)
;
}
Widget numberInputWidget(){
final buttonMargin = MediaQuery.of(context).size.width * 0.038;
return Card(
elevation: card_elevation,
color: Colors.deepPurple[50],
child: Container(
height: MediaQuery.of(context).size.height * 0.3,
child: ListView(
children: <Widget>[
ListTile(
title: Text('Partner'),
subtitle: Text('Enter the phone number'),
),
Center(
child: Container(
margin: EdgeInsets.only(left: MediaQuery.of(context).size.width * 0.04, right: MediaQuery.of(context).size.width * 0.04),
child: TextField(
controller: phoneNumber,
keyboardType: TextInputType.number,
enabled: true,
maxLength: 10,
maxLengthEnforced: true,
decoration: InputDecoration(
prefixText: '+91-',
contentPadding: EdgeInsets.only(left: 5.0, bottom: 5.0),
),
),
)),
Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom)),
Container(
margin: EdgeInsets.only(left: buttonMargin, right: buttonMargin),
child: RaisedButton(
color: Colors.deepPurple,
elevation: 0,
padding: EdgeInsets.only(bottom: 5.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: Text(
"Confirm & Proceed",
style: TextStyle(
color: Colors.white,
fontFamily: "Nunito",
fontWeight: FontWeight.bold),
),
onPressed: () async {
RegExp regExp = new RegExp(r'(^[0-9]*$)');
if (phoneNumber.text.isEmpty) {
widget.scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text("Please enter the mobile number")));
} else if (!regExp.hasMatch(phoneNumber.text) || phoneNumber.text.length < 10) {
widget.scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text("Please enter valid mobile number")));
} else {
setState(() {
widget.isInviteOption = false;
widget.isInviteSent = true;
});
}
},
),
),
],
),
),)
;
}
}