问题描述
所以,我有一个简单的单页应用程序。我在该页面上创建了一个复选框,以便在单击该复选框时转换到另一个页面。但是,复选框的 onChanged 参数出现错误。如下:
CheckBox(
value: false,onChanged: (bool newValue) {
Navigator.push(
context,new MaterialPageRoute(builder: (ctxt) => new SecondScreen()),);
})
*
返回类型“SecondScreen”不是 闭包的上下文。
因此,SecondScreen 不是无状态小部件。但是如何修改 MaterialPageRouter 以成功过渡到这个新页面。
我的第二屏如下:
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');
}
},))
);
}
}
解决方法
您在应该使用 Navigator
的地方使用小部件 Navigator.of(context)
。
您的代码应如下所示:
Checkbox(
value: false,onChanged: (bool newValue) {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
SecondScreen()));
})
,
定义 SensorPage 状态的类名的方式有误。对于您的情况,它应该是 _SensorPageState
请查看以下代码:
import 'package:flutter/material.dart';
// add the rest of packages
void main() {
runApp(MaterialApp(
home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
child: Checkbox(
value: false,onChanged: (bool newValue) {
Navigator.push(
context,new MaterialPageRoute(builder: (context) => new SensorPage()),);
}
),),);
}
}
class SensorPage extends StatefulWidget {
const SensorPage({Key key,this.device}) : super(key: key);
final BluetoothDevice device;
@override
_SensorPageState createState() => _SensorPageState();
}
class _SensorPageState 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');
}
},))
);
}
}