So, I have got a simple single page app. I created a checkbox on that page to transition to another page when the checkbox is clicked. However I get an error with the onChanged parameter of the checkbox. It is as follows:
Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.push(
context,
new MaterialPageRoute(builder: (ctxt) => new SecondScreen()),
);
})
But the last line of code gives me the following error, and I don't know how to resolve it:
*
The return type 'SecondScreen' isn't a widget as required by the
closure's context.
So, SecondScreen is not a stateless widget. But how can I modify the MaterialPageRouter to successfully transition to this new page.
My Second Screen is as follows:
class SensorPage extends StatefulWidget {
const SensorPage({Key key, this.device}) : super(key: key);
final BluetoothDevice device;
@override
_SensorPageState createState() => _SensorPageState();
}
class SecondScreen extends State<SensorPage> {
final String SERVICE_UUID = "0000ffe0-0000-1000-8000-00805f9b34fb";
final String CHARACTERISTIC_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb";
bool isReady;
Stream<List<int>> stream;
List<double> traceDust;
String _dataParser2(List<int> dataFromDevice) {
return utf8.decode(dataFromDevice);
}
@override
void initState() {
super.initState();
isReady = false;
}
@override
Widget build (BuildContext ctxt) {
Oscilloscope oscilloscope = Oscilloscope(
showYAxis: true,
padding: 0.0,
backgroundColor: Colors.black,
traceColor: Colors.white,
yAxisMax: 500.0,
yAxisMin: 0.0,
dataSet: traceDust,
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
),
body: Container(
child: !isReady
? Center(
child: Text(
"Waiting...",
style: TextStyle(fontSize: 24, color: Colors.red),
),
)
: Container(
child: StreamBuilder<List<int>>(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
if (snapshot.connectionState ==
ConnectionState.active){
var currentValue2 = _dataParser2(snapshot.data);
traceDust.add(double.tryParse(currentValue2) ?? 0);
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(flex: 1, child:Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Home',
style: TextStyle(fontSize: 14)),
Text('${currentValue2} IamHome',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 24))
]),
),
Expanded(
flex: 1,
child: oscilloscope,)
],
));
} else {
return Text('Check the stream');
}
},),
))
);
}
}
question from:
https://stackoverflow.com/questions/66061734/transitioning-from-one-page-to-another-flutter