Flutter:如何在页面之间传递点击的文本?

huangapple go评论66阅读模式
英文:

Flutter: How to pass tapped text between pages?

问题

我已经创建了两个页面
显示来自Firestore的数据

在一个页面上,它以可点击的文本形式显示集合列表
(这是一个子集合)
需要在第二个页面中显示相关子集合
请帮忙

我已经硬编码了子集合并显示了,但需要使其动态,请帮助。

英文:

I have created two pages
displays data from firestore

in one page it shows list of collections as text which is tapabble
(it is a subcollection)
need to display relevent subcollections in second page
please help

I have hard coded subcollection and displayed , but need to be dynamic please help

答案1

得分: 1

以下是代码的翻译部分:

请参考下面的示例代码以传递参数

```dart
class FirstScreen extends StatefulWidget {
  const FirstScreen({Key key}) : super(key: key);

  @override
  _FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: InkWell(
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => SecondScreen(
                  dataList: ["Data"],
                ),
              ),
            );
          },
          child: Text(
            "Second Screen",
          ),
        ),
      ),
    );
  }
}

class SecondScreen extends StatefulWidget {
  final List dataList;
  const SecondScreen({Key key, this.dataList}) : super(key: key);

  @override
  _SecondScreenState createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text(
          widget.dataList[0].toString(),
        ),
      ),
    );
  }
}

<details>
<summary>英文:</summary>

Please refer to below example code for passing arguments

class FirstScreen extends StatefulWidget {
const FirstScreen({Key key}) : super(key: key);

@override
_FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(
dataList: ["Data"],
),
),
);
},
child: Text(
"Second Screen",
),
),
),
);
}
}

class SecondScreen extends StatefulWidget {
final List dataList;
const SecondScreen({Key key, this.dataList}) : super(key: key);

@override
_SecondScreenState createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(
widget.dataList[0].toString(),
),
),
);
}
}


</details>



huangapple
  • 本文由 发表于 2023年5月30日 09:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定