Thursday, May 28, 2020

Create variable and pass data in Flutter



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

How to make your app responsive according to screen size in Flutter

Step 1 : You can use below code to take your screen width & height      double  width =  MediaQuery . of (context).size.width;      doub...