我已经尝试了以下有关Radio类的教程,但是当我单击按钮时按钮并没有改变颜色。我知道设备正在读取点击次数,因为每当单击按钮但按钮颜色没有改变时,我都会显示一条打印语句以显示。
我只是不知道问题是什么
int radioValue = -1;
void _handleRadioValueChanged (int value){
setState(() {
radioValue = value;
print(radioValue);
});
}
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Radio<int>(
activeColor: Colors.green,
value: 0,
groupValue: radioValue,
onChanged: _handleRadioValueChanged),
new Radio<int>(
activeColor: Colors.green,
value: 1,
groupValue: radioValue,
onChanged: _handleRadioValueChanged),
new Radio <int>(
activeColor: Colors.green,
value: 2,
groupValue: radioValue,
onChanged: _handleRadioValueChanged),
),
)
);
我希望按钮在被按下时会发生变化,但只是保持在值= -1的状态; </ p>
答案 0 :(得分:-1)
下面是代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int radioValue = -1;
void _handleRadioValueChanged(int value) {
setState(() {
radioValue = value;
print(radioValue);
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Column(mainAxisAlignment: MainAxisAlignment.center, children: [
new Radio(
activeColor: Colors.green,
value: 0,
groupValue: radioValue,
onChanged: _handleRadioValueChanged),
new Radio<int>(
activeColor: Colors.green,
value: 1,
groupValue: radioValue,
onChanged: _handleRadioValueChanged),
new Radio<int>(
activeColor: Colors.green,
value: 2,
groupValue: radioValue,
onChanged: _handleRadioValueChanged),
]),
));
}
}