我创建了一个类似于复选框的按钮,当您单击它时,它会更改以显示您已选择它,反之亦然以取消选择.... on/off .. true/false。
现在我已经将它导入到 main .dart.. 并调用了两次按钮(传递不同的参数以创建不同的文本)以获得两个按钮。如何在main.dart文件中区分第一个按钮和第二个按钮单击以及其中一个是打开还是关闭,并显示输出:
MaterialApp(
home: Scaffold(
body: Column(
children: [
ButtonCheck('Text1'),
ButtonCheck('Text2'),
Text('Button1 is '), // on or off
Text('Button2 is '),
],
)),
);
按钮:接受文本参数,如下所示:--GestureDetector出于设计目的使用和容器,代码被简化--
import 'package:flutter/material.dart';
class ButtonCheck extends StatefulWidget {
String text;
ButtonCheck(
this.text,
);
_ButtonCheckState createState() => _ButtonCheckState();
}
class _ButtonCheckState extends State<ButtonCheck> {
String text;
void initState() {
text = widget.text;
super.initState();
}
var _textStyle;
void ActivateButton() {
setState(() {
_textStyle = TextStyle(fontSize: 20, color: Colors.green);
});
}
void DeactivateButton() {
setState(() {
_textStyle = TextStyle(fontSize: 18, color: Colors.red);
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => setState(() {
if (_textStyle == TextStyle(fontSize: 18, color: Colors.red)) {
ActivateButton();
} else {
DeactivateButton();
}
}),
child: Text(
'${text}',
textAlign: TextAlign.center,
style: _textStyle,
),
);
}
}
