我有一個復選框,旁邊有一個文本,當用戶點擊文本時,我想要啟用/禁用復選框,我該怎么做?
Widget buildRememberCb() {
return Container(
height: 200,
child: Row(
textDirection: TextDirection.rtl,
children: [
Theme(
data: ThemeData(unselectedWidgetColor: Colors.white),
child: Checkbox(
value: isRememberMe,
checkColor: Color(0xfff44336),
activeColor: Colors.white,
onChanged: (value) {
setState(() {
isRememberMe = value!;
});
},
),
),
Text(
"Remember Me",
style: TextStyle(
fontWeight: FontWeight.w900, fontSize: 18, color: Colors.white),
),
],
),
);
}
uj5u.com熱心網友回復:
包裹TextwithGestureDetector并更改isRememberMein的值onTap,
Widget buildRememberCb() {
return Container(
height: 200,
child: Row(
textDirection: TextDirection.rtl,
children: [
Theme(
data: ThemeData(unselectedWidgetColor: Colors.white),
child: Checkbox(
value: isRememberMe,
checkColor: Color(0xfff44336),
activeColor: Colors.white,
onChanged: (value) {
setState(() {
isRememberMe = value!;
});
},
),
),
GestureDetector(
onTap: (){
setState((){
isRememberMe = isRememberMe ? false : true;
});
},
child: Text(
"Remember Me",
style: TextStyle(
fontWeight: FontWeight.w900, fontSize: 18, color: Colors.white),
),
),
],
),
);
}
如果你想要一個 Ink 飛濺,你可以用GestureDetectorwith代替,InkWell但更好的方法是使用TextButton,
TextButton(
onPressed: () {
setState((){
isRememberMe = isRememberMe ? false : true;
});
},
child: Text(
"Remember Me",
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 18,
color: Colors.white,
),
),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/329961.html
