[英]Transitioning from one page to another - Flutter
所以,我有一個簡單的單頁應用程序。 我在該頁面上創建了一個復選框,以便在單擊該復選框時轉換到另一個頁面。 但是,復選框的 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 的 State 的 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');
}
},),
))
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.