将元素存储到数组中

时间:2017-05-25 04:50:20

标签: arrays dart flutter

目前我是自我倾斜的飞镖,主要是扑动的应用程序开发。我遇到了将元素存储到字符串数组中的问题,从另一个类获取数组并成功显示。我的目标只是建立一个简单的应用程序,询问3个问题并以非常有趣的方式显示它们!

class SecondPage extends StatefulWidget
{
  @override
  SecondPageState createState() => new SecondPageState();
}

class SecondPageState extends State<SecondPage>
{
  final TextEditingController controller = new TextEditingController();
  int counter = 0;
  var ask = ["What is your name?", "How old are you?", "What is your favorite hobby?"];
  var results = ["","",""];
  @override
  Widget build(BuildContext context)
  {
    return new Scaffold(
      appBar: new AppBar(title: new Text("Tell us about yourself"), backgroundColor: Colors.deepOrange),
      body: new Container(
        padding: new EdgeInsets.all(20.0),
        child: new Center(
          child: new Column(
             mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(ask[counter], style: new TextStyle(fontSize: 15.0,  fontWeight: FontWeight.bold)),
              new TextField(
                decoration: new InputDecoration(
                  hintText: "Type Here"
                ),
                onSubmitted: (String str)
               {
                 setState(()
                 {
                   results[counter] = ask[counter]; //Assigning 
                   if(counter < 2)
                   {
                     counter = counter + 1;
                   }
                   else
                   {
                     counter = 0;
                   }
                 });
               },
              ),
              new IconButton(
                padding: new EdgeInsets.only(right: 50.0),
                icon: new Icon(Icons.library_books, size: 70.0, color:   Colors.blueAccent),
                  onPressed: ()     {Navigator.of(context).pushNamed("/ThirdPage");}
              ),
            ]
          )
        )
      )
    );
  }
}

class ThirdPageState extends State<ThirdPage>
{
  SecondPageState _second = new SecondPageState();

  // ignore: unnecessary_getters_setters
  SecondPageState get second => _second;

  // ignore: unnecessary_getters_setters, avoid_return_types_on_setters
  void set second(SecondPageState second) {
    _second = second;
  }
  @override
  Widget build(BuildContext context)
  {

    //_second.results[0] = "christopher";
    return new Scaffold(
    appBar: new AppBar(title: new Text("Your Biography"),    backgroundColor: Colors.deepOrange),
      body: new Container(
        padding: new EdgeInsets.all(20.0),
        child: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new MyCard(
                  tittle: new Text(second.results[0], style: new     TextStyle(
                      fontSize: 25.0
                  )),
                  icon: new Icon(Icons.favorite, size: 40.0, color: Colors.redAccent)
              ),
              new MyCard(
                  tittle: new Text(second.results[1], style: new TextStyle(
                      fontSize: 25.0
                  )),
                  icon: new Icon(Icons.local_pizza, size: 40.0, color: Colors.brown)
              ),
              new MyCard(
                  tittle: new Text(second.results[2], style: new TextStyle(
                      fontSize: 25.0
                  )),
                  icon: new Icon(Icons.visibility, size: 40.0, color:     Colors.blue)
          )
        ]
      )
    )
  )
);

} }

1 个答案:

答案 0 :(得分:1)

您应该致电Navigator.pushNamed,而不是致电Navigator.push。这将允许您将参数传递给ThirdPage构造函数。

Navigator.push(context, new MaterialPageRoute<Null>(
  builder: (context) => new ThirdPage(results: results);
));

您应将结果List存储为ThirdPage的最终成员,并将ThirdPageState作为widget.results访问。

通常,您应该没有SecondPageState存储对ThirdPageState的直接引用作为成员变量。虽然State可能会引用另一个状态,但您希望使用GlobalKey并调用currentState,如果可以的话,将数据作为构造函数参数传递则更加可维护。