将变量_id发送到flutter中的下一个文件

时间:2018-05-24 14:56:08

标签: dart flutter

int _id;
              List values = snapshot.data;
      return new ListView.builder(
          itemCount: values == null ? 0 : values.length,
          itemBuilder: (BuildContext context, int index) {
            print(values[index]);
            return new Card(
                child:
                    new Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
              new Image.network(values[index]["event_banner"]),
                  new Padding(padding: new EdgeInsets.only(top: 8.0),),
              new Row(
                    children: <Widget>[

                  new Icon(Icons.location_on, color: Color(0xFF53DD6C)),
                  new Text("..."),
                  new Text(values[index]["venue"].toString()
                  )),
                ],
                  ),new Padding(padding: new EdgeInsets.only(top: 15.0),),
        new Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
            new MaterialButton(
                child: new Text("CHECK IN"),
            color: Color(0xFF53DD6C),

            textColor: Colors.white,

            onPressed: ()
            {
                          _id = values[index]["id"];
                Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => new Search ()));
                }),

我想通过导航器将变量_id发送到flutter中的另一个类,但是我想知道如何传递它? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我们说我们有2个屏幕:FirstScreen和SecondScreen。下面的示例显示如何从FirstScreen向SecondScreen发送整数。通过将整数传递给SecondScreen的构造函数,它实际上很简单。

// first_screen.dart

import 'package:flutter/material.dart';
import 'package:flutter_playground/navigation/second_screen.dart';

class FirstScreen extends StatefulWidget {
  @override
  FirstScreenState createState() => new FirstScreenState();
}

class FirstScreenState extends State<FirstScreen> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(
        child: new Center(
          child: new Text("This is first screen!", style: new TextStyle(color: Colors.red, fontSize: 20.0),),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.navigate_next),
          onPressed: () {
        Navigator.push(context,
            new MaterialPageRoute(builder: (context) => new SecondScreen(5)));
      }),
    );
  }
}
// second_screen.dart

import 'package:flutter/material.dart';

class SecondScreen extends StatelessWidget {
  int id;

  SecondScreen([this.id]);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(
        child: new Center(
          child: new Text("This is the passed id: ${this.id}", style: new TextStyle(color: Colors.blue, fontSize: 20.0)),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.navigate_before),
          onPressed: () {
        Navigator.pop(context);
      }),
    );
  }
}