【问题标题】:Flutter how to pass value to State classFlutter如何将值传递给State类
【发布时间】:2020-02-05 22:56:22
【问题描述】:

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

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

不确定如何处理将变量传递给状态类。我可以使用 '${widget.email}' 但只能在小部件数组中使用。在列表选项卡中,我真的不知道怎么做。颤抖的做事方式很新……我在某个地方做错了吗?

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吗?您可以在该函数中进行初始化。
  • 关心如何分享?

标签: flutter


【解决方案1】:

你需要在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"),
          )
        ],
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 2020-12-30
    • 2021-11-11
    • 2022-09-27
    • 1970-01-01
    • 2021-09-02
    • 2013-07-13
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多