如何在 flutter 上使用另一个文件中的 initState?

How to use initState from another file on flutter?

我正在尝试导入存储我 Switch 状态的 initState 信息。

问题是 initState 在我的主页文件上,我想从我的“错误页面”导入它。

示例:

这是我的主页:

class _MyHomePageState extends State\<MyHomePage\> {
bool isSwitchedFT = false;
void updateSwitchValue(bool newValue)
  {
    setState(() {
      isSwitchedFT = newValue;
    });
  }

@override
void initState(){
super.initState();
getSwitchValues();
}
getSwitchValues() async {
isSwitchedFT = (await getSwitchState())!;
setState(() {});
}

Future <bool> saveSwitchState(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("switchState", value);
return prefs.setBool("switchState", value);
}

Future <bool?> getSwitchState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool? isSwitchedFT = prefs.getBool("switchState");

    return isSwitchedFT;

}

@override
Widget build(BuildContext context) {
return Scaffold(){...} 

这是我的另一页:

class _ErrorPageState extends State<ErrorPage> {

/// I want to get this boolean info from MyHomePage initState
   bool isSwitchedFT = false;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(...);

这是我的开关:

Switch(
                  value: isSwitchedFT,
                  onChanged: (bool value) {
                    setState(() {
                      isSwitchedFT = value;
                      saveSwitchState(value);
                      //switch works
                    });
                  })

这是我第二页的完整代码:

class SubmitErrorPage extends StatelessWidget {
  const SubmitErrorPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ErrorPage(),
    );
  }
}

class ErrorPage extends StatefulWidget {


  const ErrorPage({Key? key}) : super(key: key);

  @override
  State<ErrorPage> createState() => ErrorPageState();
}

class ErrorPageState extends State<ErrorPage> {


  bool isSwitchedFT = true;

  GlobalKey<MyHomePageState> myKey = GlobalKey();
  //get the value of the switch
  bool getSwitchValueFromHomePage()
  {
    return myKey.currentState!.isSwitchedFT;
  }
  //update the switch
  void updateSwitchValue(bool newValue)
  {
    myKey.currentState!.updateSwitchValue(newValue); // you need to implement this function in your MyHomePageState
  }



  @override
  Widget build(BuildContext context) {
    return SafeArea(
child: Scaffold(
          backgroundColor: isSwitchedFT ? Colors.black : Colors.white,){...}

您使用 GlobalKey 从您的状态访问变量,例如:

 static GlobalKey<MyHomePageState> myKey = GlobalKey();

 myKey.currentState!.isSwitchedFT; //here

您必须从您的状态中删除 _ 部分才能访问。

编辑:代码中的逻辑类似于:

class _ErrorPageState extends State<ErrorPage> {

  /// I want to get this boolean info from MyHomePage initState
  bool isSwitchedFT = false;
  GlobalKey<MyHomePageState> myKey = GlobalKey();
  //get the value of the switch 
  bool getSwitchValueFromHomePage()
  {
    return myKey.currentState!.isSwitchedFT;
  }
  //update the switch
  void updateSwitchValue(bool newValue)
  {
    myKey.currentState!.updateSwitchValue(newValue); // you need to implement this function in your MyHomePageState
  }
  
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(...);
  }
}

updateSwitchValue 看起来像:

void updateSwitchValue(bool newValue)
{
  setState(() {
       this.isSwitchedFT = newValue;
    });
}

并且您需要将其添加到您的 MyHomePageState