【问题标题】:Storing elements into an array将元素存储到数组中
【发布时间】:2017-10-25 15:08:27
【问题描述】:

目前我是自学飞镖,主要是用颤振进行应用程序开发。我遇到了将元素存储到字符串数组、从另一个类获取数组并成功显示的问题。我的目标只是构建一个简单的应用程序,它会提出 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)
          )
        ]
      )
    )
  )
);

} }

【问题讨论】:

    标签: arrays dart flutter


    【解决方案1】:

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

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

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

    通常您不应该让SecondPageState 存储对ThirdPageState 的直接引用作为成员变量。虽然一个 State 可以获取对另一个 state 的引用,但您希望使用 GlobalKey 并调用 currentState,如果可以的话,将数据作为构造函数参数传递会更易于维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-24
      • 2012-10-12
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      相关资源
      最近更新 更多