如何在 Flutter 的 IconData 中插入一个变量?
How to interpolate a variable in IconData in Flutter?
我想显示一个社交媒体的图标。
String socialMedia = 'facebook';
然后在我的构建函数中,我想在我的图标小部件中使用这个变量
Icon(FontAwesomeIcons.socialMedia)
如何插入这个变量?可能吗?
如果我可以像这样使用变量,这对我来说会变得很容易。因为现在我可以使用变量来相应地更改图标了。
您可以复制粘贴 运行 下面的完整代码
您可以使用包 https://pub.dev/packages/icons_helper
您可以使用 getIconUsingPrefix
,对于 FontAwesome
图标,您可以在字符串前加上 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),
),
);
}
}
我想显示一个社交媒体的图标。
String socialMedia = 'facebook';
然后在我的构建函数中,我想在我的图标小部件中使用这个变量
Icon(FontAwesomeIcons.socialMedia)
如何插入这个变量?可能吗? 如果我可以像这样使用变量,这对我来说会变得很容易。因为现在我可以使用变量来相应地更改图标了。
您可以复制粘贴 运行 下面的完整代码
您可以使用包 https://pub.dev/packages/icons_helper
您可以使用 getIconUsingPrefix
,对于 FontAwesome
图标,您可以在字符串前加上 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),
),
);
}
}