我如何将数据传递到扑朔迷离的多个状态页面?

发布于 2025-02-08 06:04:52 字数 390 浏览 2 评论 0原文

因此,我正在尝试将数据从一个页面传递到多个页面,但这是一个问题:我只想实际导航到其中一个。我正在使用此代码片段导航到下一页:

     Navigator.push(
             context,
             MaterialPageRoute(
                 builder: (context) =>
                     TodayPage(lg_username: unameController.text)),).then((value) => _login(lg_username));

但是现在我需要LG_USERNAME数据可以在多个其他页面中可用,而无需实际导航到它们。对我如何实现这一目标有什么想法吗?

So, I'm trying to pass data from one page to multiple pages, but here's the catch: I only want to actually navigate to one of them. I'm using this code snippet to navigate to the next page:

     Navigator.push(
             context,
             MaterialPageRoute(
                 builder: (context) =>
                     TodayPage(lg_username: unameController.text)),).then((value) => _login(lg_username));

But now I need the lg_username data to be available in multiple other pages without actually navigating to them. Any thoughts on how I could achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2025-02-15 06:04:52

尝试以下内容:

创建文件 static_variable.dart

class StaticVariable {
  static UserModel? user;
  static String? userId;
}

保存userId用户 data to staticvariable

ElevatedButton(
        child: const Text('Navigator'),
        onPressed: () {
          StaticVariable.userId = 'YOUR USER ID';
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    TodayPage(lg_username: unameController.text)),);
        },
      )

然后,您可以调用<代码>在任何文件中的staticvariable

Text(StaticVariable.userId ?? '')

Try this follow:

create file static_variable.dart

class StaticVariable {
  static UserModel? user;
  static String? userId;
}

save userId or user data to StaticVariable

ElevatedButton(
        child: const Text('Navigator'),
        onPressed: () {
          StaticVariable.userId = 'YOUR USER ID';
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    TodayPage(lg_username: unameController.text)),);
        },
      )

Then, you can call StaticVariable in any file

Text(StaticVariable.userId ?? '')
望笑 2025-02-15 06:04:52

在Ur Main.dart文件中

import 'package:flutter/material.dart';
import 'package:stackoverflow/mySecondPage.dart';
import 'myStaticVar.dart';
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'first window',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'first window'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  void onPop(){
    print("my new set var ${StaticVariable.userId}");
    setState(() {
     
    });
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'ur static var now ',
            ),
            Text(
                StaticVariable.userId.toString()
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          StaticVariable.userId = 'YOUR USER ID';
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    mySecondPage()),).then((value) => onPop());
        },
        tooltip: 'Go To Other Page',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

使用mystaticvar.dart名称创建类,然后将tihs放入内部


class StaticVariable {
  static String? userId;
}

和第二页,并带有mySecondPage.dart.dart

import 'package:flutter/material.dart';
import 'myStaticVar.dart';
class mySecondPage extends StatefulWidget {
  const mySecondPage({Key? key}) : super(key: key);

  @override
  State<mySecondPage> createState() => _mySecondPageState();
}

class _mySecondPageState extends State<mySecondPage> {
  TextEditingController myVariblaeContorler = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(leading: IconButton(onPressed: (){Navigator.pop(context);}, icon: Icon(Icons.arrow_back)),),
      body: Center(
          child: TextField(
            onSubmitted: (value){
              StaticVariable.userId = myVariblaeContorler.text;
            },
        controller: myVariblaeContorler,
      ),),
    );
  }
}

现在对其进行测试,如果您可以使用sharedPreference,即使您关闭了您的应用程序,也可以使用sharedPreference保留var然后重新开始

in ur main.dart file

import 'package:flutter/material.dart';
import 'package:stackoverflow/mySecondPage.dart';
import 'myStaticVar.dart';
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'first window',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'first window'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  void onPop(){
    print("my new set var ${StaticVariable.userId}");
    setState(() {
     
    });
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'ur static var now ',
            ),
            Text(
                StaticVariable.userId.toString()
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          StaticVariable.userId = 'YOUR USER ID';
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    mySecondPage()),).then((value) => onPop());
        },
        tooltip: 'Go To Other Page',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

creat class with myStaticVar.dart name and put tihs inside


class StaticVariable {
  static String? userId;
}

and ur second page with name of mySecondPage.dart

import 'package:flutter/material.dart';
import 'myStaticVar.dart';
class mySecondPage extends StatefulWidget {
  const mySecondPage({Key? key}) : super(key: key);

  @override
  State<mySecondPage> createState() => _mySecondPageState();
}

class _mySecondPageState extends State<mySecondPage> {
  TextEditingController myVariblaeContorler = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(leading: IconButton(onPressed: (){Navigator.pop(context);}, icon: Icon(Icons.arrow_back)),),
      body: Center(
          child: TextField(
            onSubmitted: (value){
              StaticVariable.userId = myVariblaeContorler.text;
            },
        controller: myVariblaeContorler,
      ),),
    );
  }
}

now test it , and if u whant u can use SharedPreferences to hold the var even if u turn off ur app and start it again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文