我试图让两个开关在单击时在打开和关闭之间切换。我为开关的设计创建了一个 StatelessWidget 类。但是,当我使用它并在我的用户界面类中调用该类时,开关不会改变状态。如何更新我的代码以允许进行更改?
import 'package:flutter/material.dart';
class NotificationItem extends StatelessWidget {
NotificationItem(
{@required this.title,
@required this.pushStatus,
@required this.emailStatus});
String title;
bool pushStatus;
bool emailStatus;
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.all(20),
child: Text(
title,
style: TextStyle(
fontFamily: kFontFamilyNormal,
fontSize: 17,
color: AppColor.text,
fontWeight: FontWeight.w500),
),
),
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Switch(
value: emailStatus,
onChanged: (value) {
emailStatus = value;
print(emailStatus);
},
activeTrackColor: AppColor.primaryColorDark,
activeColor: AppColor.white,
),
Switch(
value: pushStatus,
onChanged: (value) {
pushStatus = value;
print(pushStatus);
},
activeTrackColor: AppColor.primaryColorDark,
activeColor: AppColor.white,
),
],
),
),
],
);
}
}
下面的代码是我如何称呼它的:
NotificationItem(
title: 'New messages',
emailStatus: emailStatus,
pushStatus: pushStatus,
)