Flutter,如何将值从一个函数传递到不同页面上的另一个函数

Flutter, How to pass value from one function to another function on a different page

我试图在单击按钮时传递一个值,我想在代码中将值从按钮小部件传递到另一个名为 pdfNo 的页面变量。这是我的代码:

FlatButton(
          padding: EdgeInsets.fromLTRB(2, 5, 5, 5),
          child: ListTile(
            leading: Icon(Icons.book, color: Color(0xFFEB3549)),
            title: Text('Book5'),
          ),
          onPressed: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                builder: (context) => PDFPage() ));
            print('Pressed 6');
          },
        ),`

这是第一页上的按钮,按下时我想将一个值传递给一个子函数,所以我创建了一个变量,但不知道下一步该怎么做:

var pdfNo = 2;
bool _isLoading = true;
PDFDocument document;
@override
   void initState() {
      super.initState();
        changePDF(pdfNo);
        loadDocument();
         }

changePDF(value) async {
setState(() => _isLoading = true);
if (value == 1) {
  document = await PDFDocument.fromAsset('assets/sample2.pdf');
} else if (value == 2) {
  document = await PDFDocument.fromURL(
      "http://conorlastowka.com/book/CitationNeededBook-Sample.pdf");
} else {
  document = await PDFDocument.fromAsset('assets/sample.pdf');
}
setState(() => _isLoading = false);
}

所以,我想将第 1 页 Staleful Class 上的按钮的 int 传递给第二页上的 Stateful Class 以更改 PDF(此处)。

请帮帮我。 PS Flutter 新手,Dart

您可以创建一个 to 个参数...

像这样

class LoginWidgetArguments {
  final String username;

  LoginWidgetArguments(
      {this.username});
}
class LoginWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _LoginWidgetState();
}

class _LoginWidgetState extends State<LoginWidget>{
  LoginWidgetArguments args;

  @override
  void initState() {
    //Get context
    // I do this because sometimes it doesn't get the context without the FutureDelay... This workaround I used with the first flutter versions, maybe now its not needed
    Future.delayed(Duration.zero, () {
      args = ModalRoute.of(context).settings.arguments;
      if (args != null) {
        print(args.username)
      }
    });
  }

....
}

并导航

Navigator.pushNamed(context, LoginPage.routeName,
                    arguments: LoginWidgetArguments(
                        user: "user@yay.com");

编辑

也许可以很简单...

FlatButton(
          padding: EdgeInsets.fromLTRB(2, 5, 5, 5),
          child: ListTile(
            leading: Icon(Icons.book, color: Color(0xFFEB3549)),
            title: Text('Book5'),
          ),
          onPressed: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                builder: (context) => PDFPage(pageNumber: 6) ));
            print('Pressed 6');
          },
        )
class PDFPage extends StatefulWidget {
  final int pageNumber;
  PDFPage({this.pageNumber});
  @override
  _PDFPageStage createState() => _PDFPageStage();
}

class _PDFPageStage extends State<PDFPage> {
...
    @override
   void initState() {
      super.initState();
      changePDF(widget.pageNumber);
      loadDocument();
   }
...
}

我不确定我是否理解正确,但我认为你可以将数字传递给 StatefulWidget 的构造函数。为了简单起见,我将 PDFDocument 更改为 String

在第一页按下一些按钮:

onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => PDFPage(pdfNo: 4),
    ),
  );
},

第二页:

class PDFPage extends StatefulWidget {
  final int pdfNo;

  const PDFPage({Key key, this.pdfNo}) : super(key: key);
  @override
  _PDFPageState createState() => _PDFPageState();
}

class _PDFPageState extends State<PDFPage> {
  bool _isLoading = false;
  String _document;

void changePDF(int value) async {
  setState(() => _isLoading = true);
  if (value == 1) {
    _document = await Future.delayed(Duration(seconds: 1), () => 'Value 1');
  } else if (value == 2) {
    _document = await Future.delayed(Duration(seconds: 1), () => 'Value 2');
  } else {
    _document = await Future.delayed(Duration(seconds: 1), () => 'Other value');
  }
  setState(() => _isLoading = false);
}

  @override
  void initState() {
    super.initState();
    changePDF(widget.pdfNo);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Test'),),
          body: Center(
       child: _isLoading ? CircularProgressIndicator() : Text(_document ?? '(null)'), 
      ),
    );
  }
}