【发布时间】:2020-06-21 11:52:43
【问题描述】:
我正在尝试制作一个颤动的应用程序,我在这里要做的就是在用户更改 LiteRollingSwitch 时将 bool isSwitched 更改为 true 或 false。 (我使用了这个https://pub.dev/packages/lite_rolling_switch#-readme-tab- 库)。在 onchage 方法中,如果我创建“setstate”,它会给我一个如下所示的错误。
但我需要将 isSwitched 状态更改为 true 或 false 取决于用户的选择;它允许用户模糊图像或取消模糊图像。有人有解决这个问题的想法吗?
class UploadPhoto extends StatefulWidget {
@override
_UploadPhotoState createState() => _UploadPhotoState();
}
class _UploadPhotoState extends State<UploadPhoto> {
bool isSwitched = false;
File _image;
// select image via either folder of camera
Future getImageFromGallery() async {
PickedFile image =
await ImagePicker().getImage(source: ImageSource.gallery);
setState(() {
_image = File(image.path);
});
}
Future getImageFromCamera() async {
PickedFile image = await ImagePicker().getImage(source: ImageSource.camera);
setState(() {
_image = File(image.path);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: <Widget>[
Container(...),
Container(
child: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Container(
child: _image != null
? Center(
Transform.scale(
scale: 0.8,
child: LiteRollingSwitch(
value: isSwitched,
textOn: 'On',
textOff: 'Off',
colorOn: Hexcolor("#8CC63E"),
colorOff: Colors.blueGrey,
iconOn: Icons.done,
iconOff: Icons.remove_circle_outline,
onChanged: (value) {
// isSwitched = value;
setState(() {
isSwitched = value;
});
},
),
),
【问题讨论】: