[英]How to pass a string from one page to another on flutter?
如何將 image_category(a String value) 從一個頁面傳遞到另一個頁面並將其替換為Text
小部件?
這是我從( scrlView
)獲取字符串的地方:-
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(0.0),
child: GestureDetector(
onTap: () {
print("$image_category"); //TODOPass tpped text to CatagoriesBar,
},
這就是我想用文本小部件替換它的地方(這是在單獨的頁面CategoriesBar
上):-
import 'package:flutter/material.dart';
class CategoriesBar extends StatefulWidget {
@override
_CategoriesBarState createState() => _CategoriesBarState();
}
class _CategoriesBarState extends State<CategoriesBar> {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(
"All Categories",//TODOReplace with category tapped on scrlView
style: TextStyle(
color: Colors.black,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
);
}
我是 flutter 的新手,抱歉。 如果細節不夠(在評論中告訴我,我會更新問題)
我嘗試過使用Navigator.push
,它會彈出一個帶有“image_category”的新黑屏。
這是我嘗試過的: scrlView
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(0.0),
child: GestureDetector(
onTap: () {
print("$image_category");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoriesBar (categoryTitle: image_caption),
)
);
},
這是我嘗試過的:- CategoriesBar
import 'package:flutter/material.dart';
class CategoriesBar extends StatefulWidget {
final String categoryTitle;
CategoriesBar({Key key, @required this.categoryTitle}) : super(key: key);
@override
_CategoriesBarState createState() => _CategoriesBarState();
}
class _CategoriesBarState extends State<CategoriesBar> {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(
widget.categoryTitle,
style: TextStyle(
color: Colors.red,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
);
}
這就是我得到的:-
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(0.0),
child: GestureDetector(
onTap: () {
print("$image_category");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoriesBar (text: image_category),
)
);
},
…
)
)
}
接着:
import 'package:flutter/material.dart';
class CategoriesBar extends StatefulWidget {
final String text;
CategoriesBar ({Key key, @required this.text}) : super(key: key);
@override
_CategoriesBarState createState() => _CategoriesBarState();
}
class _CategoriesBarState extends State<CategoriesBar> {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(
widget.text,
style: TextStyle(
color: Colors.black,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
);
}
}
有關在 Flutter 中的屏幕之間傳遞數據的更多信息
更新
您應該將內容包裝在 Scaffold 視圖中。
import 'package:flutter/material.dart';
class CategoriesBar extends StatefulWidget {
final String categoryTitle;
CategoriesBar({Key key, @required this.categoryTitle}) : super(key: key);
@override
_CategoriesBarState createState() => _CategoriesBarState();
}
class _CategoriesBarState extends State<CategoriesBar> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("APPBAR"),
),
body: Align(
alignment: Alignment.center,
child: Text(
widget.categoryTitle,
style: TextStyle(
color: Colors.red,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
),
);
}
請確保相應地關閉括號。 我在沒有任何代碼編輯器的情況下打字。 Vs 代碼為您提供了很好的格式化和錯誤處理能力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.