简体   繁体   English

Flutter - 如何将变量从一个飞镖类传递到另一个飞镖类

[英]Flutter -How to Pass variable from one dart class to another dart class

I just want to pass my int and bool values into another class in another dart file.我只想将我的 int 和 bool 值传递给另一个 dart 文件中的另一个类。 I am trying to pass values the method.我正在尝试将值传递给方法。

Try this.尝试这个。

import 'package:flutter/material.dart';

void main() => runApp(new MaterialApp(
      home: new MainPage(),
    ));

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int count = 0;
  bool isMultiSelectStarted = false;

  void onMultiSelectStarted(int count, bool isMultiSelect) {
    print('Count: $count  isMultiSelectStarted: $isMultiSelect');
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.blue,
        title: new Text('Home'),
      ),
      body: new Center(
        child: new RaisedButton(
          child: new Text('Go to SecondPage'),
          onPressed: () {
            Navigator.of(context).push(
              new MaterialPageRoute(
                builder: (context) {
                  return new SecondPage(onMultiSelectStarted);
                },
              ),
            );
          },
        ),
      ),
    );
  }
}

class SecondPage extends StatefulWidget {
  int count = 1;
  bool isMultiSelectStarted = true;

  final Function multiSelect;

  SecondPage(this.multiSelect);

  @override
  _SecondPageState createState() => new _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new RaisedButton(
        child: new Text('Pass data to MainPage'),
        onPressed: () {
          widget.multiSelect(widget.count, widget.isMultiSelectStarted);
        },
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM