gpt4 book ai didi

Flutter如何向State类传递值

转载 作者:行者123 更新时间:2023-12-02 03:30:33 24 4
gpt4 key购买 nike

我在类之间传递参数时遇到问题。当我想将电子邮件从 MyScreen 类传递到我的 TabScreen 时,它显示:

[State]对象的配置是对应的[StatefulWidget]实例。该属性由框架在调用 [initState] 之前初始化。如果父级将树中的此位置更新为与当前配置具有相同 [runtimeType] 和 [Widget.key] 的新小部件,则框架将更新此属性以引用新小部件,然后调用 [didUpdateWidget],传递旧配置作为参数。

不确定如何处理将变量传递给状态类。我可以使用“${widget.email}”,但只能在小部件数组中使用。在列表选项卡内,我真的不知道如何。对 flutter 的做事方式相当陌生......我在某个地方做错了吗?

import 'package:flutter/material.dart';
import 'package:my_helper/tab_screen2.dart';
import 'package:my_helper/tab_screen3.dart';
import 'tab_screen.dart';

class MainScreen extends StatefulWidget {
final String email; //<-managed to get this variable from previous screen

MainScreen({Key key,this.email}) : super(key: key);

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

class _MainScreenState extends State<MainScreen> {
int currentTabIndex = 0;

List<Widget> tabs = [
TabScreen("Home", widget.email), //<- ERROR this widget.email shows error "only static member can be ...."
TabScreen2("Message"),
TabScreen3("Profile")
];

String $pagetitle = "Home";

onTapped(int index) {
setState(() {
currentTabIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text($pagetitle),
),
body: tabs[currentTabIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTapped,
currentIndex: currentTabIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text("Jobs"),
),
BottomNavigationBarItem(
icon: Icon(Icons.mail),
title: Text("Messages"),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text("Profile"),
)
],
),
);
}
}

我的选项卡屏幕

 import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class TabScreen extends StatelessWidget {
final String apptitle,email;

TabScreen(this.apptitle,this.email);

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Text(email)
],
);
}
}

最佳答案

您需要在 initState() 方法中初始化 tabs。更改您的 _MainScreenState 类以匹配以下内容:

class _MainScreenState extends State<MainScreen> {
int currentTabIndex = 0;

List<Widget> tabs;

String $pagetitle = "Home";

onTapped(int index) {
setState(() {
currentTabIndex = index;
});
}

@override
void initState() {
super.initState();
tabs = [
TabScreen("Home", widget.email),
TabScreen2("Message"),
TabScreen3("Profile"),
];
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text($pagetitle),
),
body: tabs[currentTabIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTapped,
currentIndex: currentTabIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text("Jobs"),
),
BottomNavigationBarItem(
icon: Icon(Icons.mail),
title: Text("Messages"),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text("Profile"),
)
],
),
);
}
}

关于Flutter如何向State类传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58289995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com