I a trying to pass a Value from when a button is clicked, What I want in the code is to pass the value from the Button Widget to another Page's Variable named pdfNo. Here's my code:
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');
},
),`
This is the Button on the First Page, On Pressed I would like to pass a Value to a function which is child, so I have created a variable, but cant figure out how to go about this next:
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);
}
So, I would like to pass a int from the button on page 1 Staleful Class to a Stateful Class on second page to changePDF(here).
Please help me out. PS New to Flutter,Dart
You can create a to
arguments...
Something like this
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)
}
});
}
....
}
And to navigate
Navigator.pushNamed(context, LoginPage.routeName,
arguments: LoginWidgetArguments(
user: "user@yay.com");
Edit
Maybe it could be simple...
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();
}
...
}
I'm not sure if I understand the problem correctly, but I think you can pass the number to the constructor of the StatefulWidget
. I changed the PDFDocument
to a String
for simplicity.
Some button press on first page:
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PDFPage(pdfNo: 4),
),
);
},
The second page:
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)'),
),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.