Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 308
Sharon Avatar asked Sep 20 '19 15:09

Sharon


People also ask

How do you pass data into stateful widget flutter?

Steps to Pass Data to Stateful Widget in Flutter To pass data to stateful widget, first of all, create two pages. Now from the first page open the second page and pass the data. Inside the second page, access the variable using the widget. For example widget.

How do I pass data from one screen to another flutter?

Here we will assign a callback function to the onTap() function that uses the Navigator. push() method of the Navigator class to pass the data to the description screen as shown below: Dart.


2 Answers

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),           ],         ),       ),     );   } } 
like image 75
Rodrigo Bastos Avatar answered Sep 19 '22 18:09

Rodrigo Bastos


For this, we create a StatelessWidget. We call it TodosScreen. Since the contents of this page won’t change during runtime, we’ll have to require the list of todos within the scope of this widget.

We pass in our ListView.builder as the body of the widget we’re returning to build(). This renders the list onto the screen for you to get going!

class TodosScreen extends StatelessWidget {   final List<Todo> todos;    //requiring the list of todos   TodosScreen({Key key, @required this.todos}) : super(key: key);    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text('Todos'),       ),       //passing in the ListView.builder       body: ListView.builder(         itemCount: todos.length,         itemBuilder: (context, index) {           return ListTile(             title: Text(todos[index].title)           );         },       ),     );   } } 

Create a detailed screen to display information about a todo

Now, create the second screen. The title of the screen contains the title of the todo, and the body of the screen shows the description.

Since the detail screen is a normal StatelessWidget, require the user to enter a Todo in the UI. Then, build the UI using the given todo.

class DetailScreen extends StatelessWidget {   // Declare a field that holds the Todo.   final Todo todo;    // In the constructor, require a Todo.   DetailScreen({Key key, @required this.todo}) : super(key: key);    @override   Widget build(BuildContext context) {     // Use the Todo to create the UI.     return Scaffold(       appBar: AppBar(         title: Text(todo.title),       ),       body: Padding(         padding: EdgeInsets.all(16.0),         child: Text(todo.description),       ),     );   } } 

Navigate and pass data to the detail screen

With a DetailScreen in place, you’re ready to perform the Navigation. In this example, navigate to the DetailScreen when a user taps a todo in the list. Pass the todo to the DetailScreen.

ListView.builder(   itemCount: todos.length,   itemBuilder: (context, index) {     return ListTile(       title: Text(todos[index].title),       // When a user taps the ListTile, navigate to the DetailScreen.       // Notice that you're not only creating a DetailScreen, you're       // also passing the current todo to it.       onTap: () {         Navigator.push(           context,           MaterialPageRoute(             builder: (context) => DetailScreen(todo: todos[index]),           ),         );       },     );   }, ); 

For more info, see https://flutter.dev/docs/cookbook/navigation/passing-data

like image 43
Paresh Mangukiya Avatar answered Sep 18 '22 18:09

Paresh Mangukiya