从一页过渡到另一页 - Flutter
Transitioning from one page to another - Flutter
所以,我得到了一个简单的单页应用程序。我在该页面上创建了一个复选框,以便在单击该复选框时转换到另一个页面。但是,复选框的 onChanged 参数出现错误。具体如下:
Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.push(
context,
new MaterialPageRoute(builder: (ctxt) => new SecondScreen()),
);
})
但是最后一行代码报了如下错误,不知如何解决:
*
The return type 'SecondScreen' isn't a widget as required by the
closure's context.
因此,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 状态的 class 名称的方式有误。对于您的情况,它应该是 _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');
}
},),
))
);
}
}
所以,我得到了一个简单的单页应用程序。我在该页面上创建了一个复选框,以便在单击该复选框时转换到另一个页面。但是,复选框的 onChanged 参数出现错误。具体如下:
Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.push(
context,
new MaterialPageRoute(builder: (ctxt) => new SecondScreen()),
);
})
但是最后一行代码报了如下错误,不知如何解决:
*
The return type 'SecondScreen' isn't a widget as required by the closure's context.
因此,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 状态的 class 名称的方式有误。对于您的情况,它应该是 _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');
}
},),
))
);
}
}