4

This is the page of my setting menu widget page or code

class Setting extends StatelessWidget {
  Setting(this.title);

  String title;


  @override
  Widget build(BuildContext context) {
     return Scaffold(
        appBar: AppBar(
        title: Text(title),
        backgroundColor: Theme.of(context).primaryColor,
       ),
     body: Column(
     children: <Widget>[
      setttingWidget(Icons.favorite),
     ],
   )
  );
 }
}

And this is settingWidget page or code :

class setttingWidget extends StatelessWidget {

  setttingWidget(IconData next);

  var next;

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Row(
        children: <Widget>[
          Icon(next),
          Text("Network Setting",
          style: TextStyle(
            fontSize: 20
          ),)
        ],
      ),
    );
  }
}

I want set the icon image from the setting Widget, so use icon data but no icon is appear in app, there the code also show no error at all.

enter image description here

2 Answers 2

7

Do it like this,

class Setting extends StatelessWidget {

final title;
  Setting({this.title});

  


  @override
  Widget build(BuildContext context) {
     return MaterialApp(home:Scaffold(
        appBar: AppBar(
        title: Text(title),
        backgroundColor: Theme.of(context).primaryColor,
       ),
     body: Column(
     children: <Widget>[
      setttingWidget(Icons.favorite),
     ],
   )
  ));
 }
}

Setting widget-

class setttingWidget extends StatelessWidget {
final IconData next;
  setttingWidget({this.next});

  

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Row(
        children: <Widget>[
          Icon(next),
          Text("Network Setting",
          style: TextStyle(
            fontSize: 20
          ),)
        ],
      ),
    );
  }
}

You have to use final keyword because stateless widgets are immutable, that means they can not change their state.

Sign up to request clarification or add additional context in comments.

3 Comments

Please upvote and accept this as the answer if it helped :)
Thank you so much, I got the point. That's is really helpfull.
Glad it worked, happy coding! :)
3

You can try my code below :

class Setting extends StatelessWidget {
  Setting(this.title);

  final String title;

  @override
  Widget build(BuildContext context) {
     return Scaffold(
        appBar: AppBar(
        title: Text(title),
        backgroundColor: Theme.of(context).primaryColor,
       ),
     body: Column(
     children: <Widget>[
      setttingWidget(Icons.favorite),
     ],
   )
  );
 }
}

class setttingWidget extends StatelessWidget {
  final IconData next;
  
  setttingWidget(this.next);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Row(
        children: <Widget>[
          Icon(next),
          Text("Network Setting",
          style: TextStyle(
            fontSize: 20
          ),)
        ],
      ),
    );
  }
}

Note : If a widget is stateless, one or more of its instance fields must be final. So,

Change this :

var next;

to

final IconData next;

Output :

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.