In Flutter, how do I pass data into a Stateless Widget?

Sharon

I'm trying to build my first Flutter app, and have run into difficulty with passing data into Stateless Widgets. I have the following classes:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'App Title',
      theme: new ThemeData(
        primarySwatch: Colors.green,
      ),
      home: new MainBody(),
    );
  }
}


class MainBody extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Padding(
        padding: const EdgeInsets.only(left: 20.0, right: 20.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new TimeCheck(),
          ],
        ),
      ),
    );
  }
}

TimeCheck is a Stateful Widget. Basically, I want to be able to set some values at the start, and then pass them along to TimeCheck, via MainBody. Everything I read shows how to pass data into Stateful Widgets, but is it possible to pass it through a Stateless Widget?

I recognise I may be taking the wrong approach, so in case it helps, what I'd ultimately like to do is have a "settings" page where the user can set the values for the data (a string and some numbers), and then those values get passed to TimeCheck where they're used to generate the display.

I'd also like to be able to save the user-set values if possible, so they don't have to enter them each time, and then I'd like to be able to read them in when TimeCheck is instantiated.

Rodrigo Bastos

Yes you can do this simply by passing the info to the constructor. Something like this:

 class MyApp extends StatelessWidget {
  MyApp(this.yourData);

  final int yourData;
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'App Title',
      theme: new ThemeData(
        primarySwatch: Colors.green,
      ),
      home: new MainBody(yourData),
    );
  }
}


class MainBody extends StatelessWidget {
  MainBody(this.yourData);

  final int yourData;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Padding(
        padding: const EdgeInsets.only(left: 20.0, right: 20.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new TimeCheck(yourData),
          ],
        ),
      ),
    );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass data from a stateful widget to a stateless in flutter?

How to pass an object AND a callback to a Stateless Widget in Flutter?

Pass params to stateless widget in Flutter

How to toggle in Flutter Stateless widget

how to do alot of calculation once in flutter stateless widget

In Flutter, how can I define a baseline for a Stateless widget?

How do I access BuildContext outside of a stateful or stateless widget?

I clicked an event on one screen and want to pass the data of that specific event from a stateless widget to a stateful widget

Flutter passing data up through stateless widget

How do I save data from a stateful widget in Flutter?

How i can pass Data from one widget to another widget in flutter?

Flutter: void function with parameter can't be executed when I pass to 2 stateless widget

How to pass json data to flutter widget

How am I able to pass setState in a stateless widget from a statefull widget

How to pass a GlobalKey through Stateless Widget Children

How do I pass properties to stateless/functional components?

How to pass data from a child Stateful widget to Parent Widget in Flutter

How do I pass data from a widget config class to a widget provider class via intents?

How do I refer to widget.function() in the build method of a stateless widget?

Flutter: How to convert a stateful widget to a stateless widget and keep state changes

how to get the id from statefull widget to stateless widget in flutter?

How can I pass the controller data from one widget to another in flutter

Flutter: How can I make an button to show a random by me defined stateless Widget?

TextField do not pass data to another flutter Widget in same class

data is not getting render in Text() widget,how do i view data in my Flutter app

How can i pass data from a child widget to a parent widget

How do I convert a DropdownButton widget into a listview widget in flutter

Access values from constructor, from passing data to stateless widget Flutter

How do you create a list of objects to pass to a widget in Flutter?