簡體   English   中英

Flutter - 如何將變量從一個飛鏢類傳遞到另一個飛鏢類

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

我只想將我的 int 和 bool 值傳遞給另一個 dart 文件中的另一個類。 我正在嘗試將值傳遞給方法。

嘗試這個。

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