Step 1 : Have to declare the variable in create the constructor.
class MyHomePage extends StatelessWidget {
final String title;
final String bodyText;
MyHomePage({Key key, this.title, this.bodyText}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: Text(
bodyText,
)),
);
}
}
Step 2: Calling & pass the data
class app extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(
title: 'sample title',
bodyText: 'this is the body',
),
);
}
}
Check out the full code
import 'package:flutter/material.dart';
void main() {
runApp(app());
}
class app extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(
title: 'sample title',
bodyText: 'this is the body',
),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
final String bodyText;
MyHomePage({Key key, this.title, this.bodyText}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: Text(
bodyText,
)),
);
}
}
No comments:
Post a Comment