Flutter 如何使用flatter中的类在按钮小部件中添加图标属性
我创建了一个RoundedButton类,并在需要按钮的地方调用该类。我为按钮添加了所有必需的属性。这是我的班级Flutter 如何使用flatter中的类在按钮小部件中添加图标属性,flutter,button,icons,Flutter,Button,Icons,我创建了一个RoundedButton类,并在需要按钮的地方调用该类。我为按钮添加了所有必需的属性。这是我的班级 import 'package:flutter/material.dart'; class RoundedButton extends StatelessWidget { final String text; final Function press; final Color color, textColor; final Icons icon; const R
import 'package:flutter/material.dart';
class RoundedButton extends StatelessWidget {
final String text;
final Function press;
final Color color, textColor;
final Icons icon;
const RoundedButton({
Key key,
this.text,
this.press,
this.color = Colors.grey,
this.icon,
this.textColor = Colors.white,
}) : super(key: key);
@override
Widget build(BuildContext context) {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
return Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Color(0xff01A0C7),
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: press,
child:Row(
children: <Widget>[
Icon(Icons.icon)),
Text(text,
textAlign: TextAlign.left,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)
),
],
)
// child: Text(text,
// textAlign: TextAlign.left,
// style: style.copyWith(
// color: Colors.white, fontWeight: FontWeight.bold)
// ),
),
);
}
}
因此,当我调用这个RoundedButton时,我可以根据需要更改每个按钮的属性值,我也想更改按钮的图标,所以我的问题是我没有访问图标上的图标变量图标(Icons.icon)
请帮助完成此操作。只需将最终图标图标
更改为最终图标
并将图标(Icons.Icon)
替换为图标
像这样:
import 'package:flutter/material.dart';
class RoundedButton extends StatelessWidget {
final String text;
final Function press;
final Color color, textColor;
final Icon icon;
const RoundedButton({
Key key,
this.text,
this.press,
this.color = Colors.grey,
this.icon = const Icon(Icons.ac_unit),
this.textColor = Colors.white,
}) : super(key: key);
@override
Widget build(BuildContext context) {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
return Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Color(0xff01A0C7),
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: press,
child: Row(
children: <Widget>[
icon,
Text(text,
textAlign: TextAlign.left,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)),
],
)
// child: Text(text,
// textAlign: TextAlign.left,
// style: style.copyWith(
// color: Colors.white, fontWeight: FontWeight.bold)
// ),
),
);
}
}
导入“包装:颤振/材料.省道”;
类RoundedButton扩展了无状态小部件{
最终字符串文本;
最终功能压力机;
最终颜色,文本颜色;
最终图标;
常数圆形按钮({
关键点,
这个文本,
本报,
this.color=Colors.grey,
this.icon=常量图标(Icons.ac_单位),
this.textColor=Colors.white,
}):super(key:key);
@凌驾
小部件构建(构建上下文){
TextStyle=TextStyle(fontFamily:'Montserrat',fontSize:20.0);
退货(
标高:5.0,
边界半径:边界半径。圆形(30.0),
颜色:颜色(0xff01A0C7),
子:材质按钮(
minWidth:MediaQuery.of(context).size.width,
填充:来自LTRB(20.0,15.0,20.0,15.0)的边缘设置,
按:按,
孩子:排(
儿童:[
偶像
文(文,,
textAlign:textAlign.left,
style:style.copyWith(
颜色:Colors.white,fontwweight:fontwweight.bold),
],
)
//子:文本(文本,
//textAlign:textAlign.left,
//style:style.copyWith(
//颜色:Colors.white,fontwweight:fontwweight.bold)
// ),
),
);
}
}
解决方案
刚刚添加了这个语句icon==null?容器():Amin Al-jebbeh解决方案上的图标,
import 'package:flutter/material.dart';
//import 'package:attendance_system_app/new.dart';
class RoundedButton extends StatelessWidget {
final String text;
final Function press;
final Color color, textColor;
final Icon icon;
const RoundedButton({
Key key,
this.text,
this.press,
this.color = Colors.grey,
this.icon,
this.textColor = Colors.white,
}) : super(key: key);
@override
Widget build(BuildContext context) {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
return Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Color(0xff01A0C7),
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: press,
child:Row(
children: <Widget>[
icon == null ? Container() :
icon,
Text(text,
textAlign: TextAlign.left,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)
),
],
)
// child: Text(text,
// textAlign: TextAlign.left,
// style: style.copyWith(
// color: Colors.white, fontWeight: FontWeight.bold)
// ),
),
);
}
}
导入“包装:颤振/材料.省道”;
//导入“包:考勤系统应用程序/new.dart”;
类RoundedButton扩展了无状态小部件{
最终字符串文本;
最终功能压力机;
最终颜色,文本颜色;
最终图标;
常数圆形按钮({
关键点,
这个文本,
本报,
this.color=Colors.grey,
这个图标,
this.textColor=Colors.white,
}):super(key:key);
@凌驾
小部件构建(构建上下文){
TextStyle=TextStyle(fontFamily:'Montserrat',fontSize:20.0);
退货(
标高:5.0,
边界半径:边界半径。圆形(30.0),
颜色:颜色(0xff01A0C7),
子:材质按钮(
minWidth:MediaQuery.of(context).size.width,
填充:来自LTRB(20.0,15.0,20.0,15.0)的边缘设置,
按:按,
孩子:排(
儿童:[
icon==null?容器()
偶像
文(文,,
textAlign:textAlign.left,
style:style.copyWith(
颜色:Colors.white,fontwweight:fontwweight.bold)
),
],
)
//子:文本(文本,
//textAlign:textAlign.left,
//style:style.copyWith(
//颜色:Colors.white,fontwweight:fontwweight.bold)
// ),
),
);
}
}
it打印此错误,行的子行不能包含任何空值,但在索引0处找到空值,当我调用类似于此RoundedButton的RoundedButton(图标:icon(Icons.timer\u off)、文本:“超时”、颜色:Colors.white、)时,您可以检查图标是否为空放置一个类似此图标的空容器==null?容器():图标,
import 'package:flutter/material.dart';
//import 'package:attendance_system_app/new.dart';
class RoundedButton extends StatelessWidget {
final String text;
final Function press;
final Color color, textColor;
final Icon icon;
const RoundedButton({
Key key,
this.text,
this.press,
this.color = Colors.grey,
this.icon,
this.textColor = Colors.white,
}) : super(key: key);
@override
Widget build(BuildContext context) {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
return Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Color(0xff01A0C7),
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: press,
child:Row(
children: <Widget>[
icon == null ? Container() :
icon,
Text(text,
textAlign: TextAlign.left,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)
),
],
)
// child: Text(text,
// textAlign: TextAlign.left,
// style: style.copyWith(
// color: Colors.white, fontWeight: FontWeight.bold)
// ),
),
);
}
}