简体   繁体   English

将数据发送到另一个 flutter 屏幕

[英]send data to another flutter screen

I need this screen to receive the name (winner X) of the winner that is sent from another screen how can I do this?我需要这个屏幕来接收从另一个屏幕发送的获胜者的姓名(获胜者 X)我该怎么做?

on the previous screen I ask the user to type the name and I made an if, if he wins his name appears, I tried with the push navgator and I couldn't在上一个屏幕上,我要求用户输入名称,然后我做了一个如果,如果他赢得了他的名字出现,我尝试使用推送导航器,但我不能

import 'package:flutter/material.dart';

class ganhadorScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Flutter Card Example')),
        backgroundColor: Colors.yellow,
        body: MyCardWidget(),
      ),
    );
  }
}

class MyCardWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Container(
        width: 300,
        height: 500,
        padding: const EdgeInsets.all(10.0),
        child: Card(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15.0),
          ),
          color: Colors.red,
          elevation: 10,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              const ListTile(
                title: Text('PARABENS', style: TextStyle(fontSize: 30.0)),
              ),
              Image.asset("assets/images/trofeu.png"),
              Text('winner X',
                  style: TextStyle(fontSize: 18.0))
            ],
          ),
        ),
      ),
    );
  }
}

You can pass data as arguments to another class like this:您可以将数据作为 arguments 传递给另一个 class,如下所示:

class MyClass extends StatelessWidget {
  final String data;
  final int number;
  const MyClass({required this.data, required this.number});
  // or like this if you don't like named arguments:
  const MyClass(this.data, this.number);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

So if the navigator pushes MyClass widget to the screen, you will have to write it like this:因此,如果导航器将 MyClass 小部件推送到屏幕上,您将不得不这样编写:

MyClass(data: "some data", number: -1);
// or without named parameters, then like this:
MyClass("some data", -1); //note that without named parameters, the order of the parameters is important!

You can pass the values through the arguments property of navigation.您可以通过导航的 arguments 属性传递值。 In the navigation action of the previous page, however you do it,在上一页的导航动作中,不管你怎么做,

Navigator.of(context).pushNamed('MyCardWidget', arguments: variable-that-holds-users-name);

In this screen you recieve the text在此屏幕中,您收到文本

final name = ModalRoute.of(context)!.settings.arguments.toString();

Now you can use it现在你可以使用它了

Text(name, style: TextStyle(fontSize 17)),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM