如何在Flutter中调用来自不同类的变量

时间:2020-03-01 10:58:50

标签: flutter dart

我希望该程序做三件事。一,显示我的号码的当前值。第二,显示我之前输入的金额。三,输入要从数字的原始值中减去的数量。

但是我的代码有一个问题。在SubtractState类中,我的FlatButton有一个onPressed函数。里面有一个名为enterentValue的变量。当我尝试在“类型”类中调用此变量时,显然给了我错误。

我知道我需要找出一种从其他类中调用变量的方法。但是我应该怎么做?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: Center(
                child: GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Type()),
        );
      },
      child: Text(
        value.toString(),
        style: TextStyle(
          fontSize: 50.0,
        ),
      ),
    ))));
  }
}

class Type extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
            body: Container(
                child: Center(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
              GestureDetector(
                child: Text("You Entered ${globals.enteredValue} previously"),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Subtract()),
                  );
                },
              )
            ])))));
  }
}

class Subtract extends StatefulWidget {
  @override
  SubtractState createState() => SubtractState();
}

class SubtractState extends State<Subtract> {
  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              elevation: 0,
              leading: IconButton(
                  color: Colors.black,
                  icon: Icon(Icons.arrow_back_ios),
                  onPressed: () => Navigator.pop(context)),
            ),
            body: Container(
                padding: const EdgeInsets.all(30.0),
                child: Container(
                  child: Center(
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                        TextFormField(
                            textInputAction: TextInputAction.done,
                            controller: _controller,
                            keyboardType: TextInputType.number),
                        FlatButton(
                          child: Text("Subtract"),
                          onPressed: () {
                            //check if we can parse it
                            if (int.tryParse(_controller.text) == null)
                              return; //can't parse it
                            globals.enteredValue = int.parse(_controller.text);
                            setState(() {
                              value -= globals.enteredValue;
                            });
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => MyApp()),
                            );
                          },
                        ),
                      ])),
                ))));
  }
}

int value = 0;

1 个答案:

答案 0 :(得分:1)

我建议使用一种简单的解决方案来使用全局库在类之间共享您的应用程序变量:
创建名为globals.dart

的飞镖文件

globals.dart:

library YOUR_PACKAGE_NAME.globals;
int x = 1 ; 

class1:

 import 'package:YOUR_PACKAGE_NAME/globals.dart' as globals;
 print(globals.x) ; //prints 1 
 globals.x= 2 ; 

在您的课堂2中:

import 'package:YOUR_PACKAGE_NAME/globals.dart' as globals;
print(globals.x) ; //prints 2