首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从一页过渡到另一页-颤振

从一页过渡到另一页-颤振
EN

Stack Overflow用户
提问于 2021-02-05 10:38:34
回答 2查看 166关注 0票数 0

所以,我有一个简单的单页应用程序。我在该页上创建了一个复选框,以在单击复选框时切换到另一个页面。但是,我在复选框的onChanged参数中出现了一个错误。其内容如下:

代码语言:javascript
复制
Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.push(
context,
new MaterialPageRoute(builder: (ctxt) => new SecondScreen()),
);
})

但是最后一行代码给出了以下错误,我不知道如何解决它:

*

返回类型'SecondScreen‘不是闭包上下文所要求的小部件。

因此,SecondScreen不是一个无状态小部件。但是如何修改MaterialPageRouter以成功地转换到这个新页面。

我的第二个屏幕如下:

代码语言:javascript
复制
   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');
                }
              },),
          ))
    );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-05 10:45:29

您使用的是小部件Navigator,应该在其中使用Navigator.of(context)

您的代码应该如下所示:

代码语言:javascript
复制
Checkbox(
    value: false, onChanged: (bool newValue) {
    Navigator.of(context).push(MaterialPageRoute(
                          builder: (BuildContext context) =>
                              SecondScreen()));
})
票数 2
EN

Stack Overflow用户

发布于 2021-02-05 11:10:00

在定义SensorPage的状态的类名时出现了一个错误。对于您的情况,应该是_SensorPageState

请查看下面的代码:

代码语言:javascript
复制
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');
                  }
                },),
            ))
    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66061734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档