![](/img/trans.png)
[英]How to pass String type parameter in function to IconData in Flutter
[英]How to interpolate a variable in IconData in Flutter?
我想展示社交媒体的图标。
String socialMedia = 'facebook';
然后在我的构建 function 我想在我的图标小部件中使用这个变量
Icon(FontAwesomeIcons.socialMedia)
我怎样才能插入这个变量? 可能吗? 如果我可以像这样使用变量,对我来说会变得很容易。 因为现在我可以使用变量来相应地更改图标。
您可以在下面复制粘贴运行完整代码
您可以使用 package https://pub.dev/packages/icons_helper
您可以使用getIconUsingPrefix
,对于FontAwesome
Icon,您可以使用fa.
代码片段
Icon(getIconUsingPrefix(name: 'fa.fiveHundredPx'),
color: Theme.of(context).backgroundColor, size: 128.0),
Icon(getIconUsingPrefix(name: 'fa.facebook'),
color: Theme.of(context).backgroundColor, size: 128.0),
Icon(getIconUsingPrefix(name: 'add'),
color: Theme.of(context).backgroundColor, size: 128.0),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:icons_helper/icons_helper.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(getIconUsingPrefix(name: 'fa.fiveHundredPx'),
color: Theme.of(context).backgroundColor, size: 128.0),
Icon(getIconUsingPrefix(name: 'fa.facebook'),
color: Theme.of(context).backgroundColor, size: 128.0),
Icon(getIconUsingPrefix(name: 'add'),
color: Theme.of(context).backgroundColor, size: 128.0),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.