Flutter 将图标参数作为构造函数传递

saz*_*min 3 parameters icons android widget flutter

这是我的设置菜单小部件页面或代码的页面

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),
     ],
   )
  );
 }
}
Run Code Online (Sandbox Code Playgroud)

这是设置Widget页面或代码:

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
          ),)
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我想从设置小部件设置图标图像,因此使用图标数据,但应用程序中没有出现图标,代码也没有显示任何错误。

在此输入图像描述

Mad*_*ahi 7

像这样做,

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),
     ],
   )
  ));
 }
}
Run Code Online (Sandbox Code Playgroud)

设置小部件-

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
          ),)
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

您必须使用final关键字,因为无状态小部件是不可变的,这意味着它们无法更改其状态。