从一个页面过渡到另一个页面-颤振



所以,我有一个简单的单页应用程序。我在该页上创建了一个复选框,以便在单击复选框时过渡到另一个页面。然而,我得到一个错误与复选框的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.of(context)的地方使用了小部件Navigator

你的代码应该如下所示:

Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
SecondScreen()));
})

如何定义SensorPage的State的类名有一个错误。对于您的情况,它应该是_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');
}
},),
))
);
}
}

最新更新