Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

android - Transitioning from one page to another - Flutter

So, I have got a simple single page app. I created a checkbox on that page to transition to another page when the checkbox is clicked. However I get an error with the onChanged parameter of the checkbox. It is as follows:

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

But the last line of code gives me the following error, and I don't know how to resolve it:

*

The return type 'SecondScreen' isn't a widget as required by the closure's context.

So, SecondScreen is not a stateless widget. But how can I modify the MaterialPageRouter to successfully transition to this new page.

My Second Screen is as follows:

   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');
                }
              },),
          ))
    );
  }
}
question from:https://stackoverflow.com/questions/66061734/transitioning-from-one-page-to-another-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you're using the widget Navigator where you should use Navigator.of(context).

You code should look as bellow:

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



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...