heelp me I want to send text from the first page to the second page.
Firts Page
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: RaisedGradientButton(
child: Text(
'Dapatkan Pengukuran',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontFamily: 'Nunito',
fontWeight: FontWeight.bold),
),
gradient: LinearGradient(
colors: const <Color>[Colors.purple, Colors.blue],
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ShowMeasurementsPage(
name: name.text,
nohap: nohap.text,
),
),
);
}),
)
Second Page*
and i want showing text this second page but error.
import 'package:flutter/material.dart';
class ShowMeasurementsPage extends StatefulWidget {
static String tag = 'show-measurements-page';
const ShowMeasurementsPage({Key? key}) : super(key: key);
@override
_ShowMeasurementsPageState createState() => _ShowMeasurementsPageState();
}
class _ShowMeasurementsPageState extends State<ShowMeasurementsPage> {
final String? name, nohap;
_ShowMeasurementsPageState({this.name, this.nohap});
can anyone help me?
CodePudding user response:
You'll need to pass the arguments to the StatefulWidget, then you can access them in the build
method by using widget.name
, or widget.nohap
.
For example:
class ShowMeasurementsPage extends StatefulWidget {
static String tag = 'show-measurements-page';
final String? name;
final String? nohap;
const ShowMeasurementsPage({
Key? key,
this.name,
this.nohap,
}): super(key: key);
@override
State<ShowMeasurementsPage> createState() =>
_ShowMeasurementsPageState();
}
class _ShowMeasurementsPageState extends State<ShowMeasurementsPage> {
@override
Widget build(BuildContext context) {
return Column(
children: [
if (widget.name != null)
Text(widget.name!),
if (widget.nohap != null)
Text(widget.nohap!),
],
);
}
}