简体   繁体   English

如何在 flutter 上将字符串从一页传递到另一页?

[英]How to pass a string from one page to another on flutter?

How can I pass image_category(a String value) from one page to another and replace it with a Text widget?如何将 image_category(a String value) 从一个页面传递到另一个页面并将其替换为Text小部件?

This is where I get the String from ( scrlView ):-这是我从( 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, 
    },

and this is where I want to replace it with the text widget(this is on a separate page CategoriesBar ):-这就是我想用文本小部件替换它的地方(这是在单独的页面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,
          ),
        ),
      );
    }

I'm a newbie to flutter, apologies.我是 flutter 的新手,抱歉。 if details aren't enough(let me know in the comments I'll update the question)如果细节不够(在评论中告诉我,我会更新问题)

Update更新

I've tried with Navigator.push and it pops a new black screen with the "image_category" on it.我尝试过使用Navigator.push ,它会弹出一个带有“image_category”的新黑屏。

Here is what I've tried:- scrlView这是我尝试过的: 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),
                )
            );
},

Here is what I've tried:- CategoriesBar这是我尝试过的:- 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,
      ),
    ),
  );
}

and this is what I got:-这就是我得到的:-

错误图片

@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),
                    )
                );
            },
        … 
        )
    )
}

And then:接着:

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,
                ),
            ),
        );
    }
}

More info on Passing data between screens in Flutter有关在 Flutter 中的屏幕之间传递数据的更多信息

UPDATE更新

You should wrap your content in a Scaffold view.您应该将内容包装在 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,
      ),
    ),
  ),
 );
}

Please make sure to close brackets relevantly.请确保相应地关闭括号。 I typed without any code editor.我在没有任何代码编辑器的情况下打字。 Vs code gives you nice formatting and error handling capabilities. Vs 代码为您提供了很好的格式化和错误处理能力。

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

相关问题 如何使用 flutter 将数据从一页传递到另一页? - How to pass data from one page to another with flutter? 如何在颤振中将多个参数从一个页面传递到另一个页面 - How to pass many parameters from one page to another in flutter 如何将变量值从一个颤动页面传递到另一个页面 - how to pass a variable value from one flutter page to another 在 Flutter 中将数据从一个页面传递到另一个页面 - Pass data from one page to another in Flutter 如何在颤振中将字符串从应用程序传递到另一个应用程序? - How to pass string from app to another in flutter? 如何在 flutter dart 中将图像选择器文件从一页传递到另一页? - How to pass image picker file from one page to another in flutter dart? Flutter,如何将值从一个 function 传递到另一页上的另一个 function - Flutter, How to pass value from one function to another function on a different page 如何将 onTap() 的值从页面传递到另一个 dart/flutter - how to pass value of onTap() from a page to another dart/flutter Flutter - 如何从另一页面调用一个页面的功能 - Flutter - How to Invoke a Function of one page from another page 如何将数据从登录页面传递到 flutter 中的另一个 url? - how to pass data from login page to another url in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM