如何在Flutter中动态传递IconData?

我正在制作一个学习Flutter的示例应用程序。我想要的是在标题上面显示一个图标。Category类负责构建这个widget。

class Category extends StatelessWidget {
  final String title;
  final IconData icon;

  Category({@required this.title, @required this.icon});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.blue,
        height: 100.0,
        width: 200.0,
        child: InkWell(
          onTap: () {
            print('$title was tapped!');
          },
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Icon(icon, color: Colors.white, size: 10,), // <===== My problem is here
                Text(
                  this.title,
                  style: TextStyle(fontSize: 24),
                )
              ],
            )
          ),
        ),
      ),
    );
  }
}

我像这样创建这个widget,并把它传递给ListView.builder。

var _categories = [
    Category(title: "Test 1", icon: Icons.airline_seat_flat),
    Category(title: "Test 2", icon: Icons.airline_seat_flat_angled),
    Category(title: "Test 3", icon: Icons.airline_seat_individual_suite),
    Category(title: "Test 4", icon: Icons.airline_seat_legroom_extra)
  ];

它看起来像这样。enter image description here

然而,如果我把上面这行替换成这行,我就能看到图标。

Icon(Icons.star, color: Colors.white, size: 20,),

enter image description here

所以,我的问题是,我怎样才能把图标从外部的widget传递到这个widget?谢谢您。

解决方案:

你可以在下面复制粘贴运行完整的代码 你的问题中显示的代码工作良好,我只添加了 ListView.builder 第一部分将图标大小改为30的演示代码片段

child: ListView.builder(
              padding: const EdgeInsets.all(8),
              itemCount: _categories.length,
              itemBuilder: (BuildContext context, int index) {
                return _categories[index];
              }),

工作示范

enter image description here

全码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class Category extends StatelessWidget {
  final String title;
  final IconData icon;

  Category({@required this.title, @required this.icon});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.blue,
        height: 100.0,
        width: 200.0,
        child: InkWell(
          onTap: () {
            print('$title was tapped!');
          },
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Icon(
                    icon,
                    color: Colors.white,
                    size: 30,
                  ), // <===== My problem is here
                  Text(
                    this.title,
                    style: TextStyle(fontSize: 24),
                  )
                ],
              )),
        ),
      ),
    );
  }
}

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;
  var _categories = [
    Category(title: "Test 1", icon: Icons.airline_seat_flat),
    Category(title: "Test 2", icon: Icons.airline_seat_flat_angled),
    Category(title: "Test 3", icon: Icons.airline_seat_individual_suite),
    Category(title: "Test 4", icon: Icons.airline_seat_legroom_extra)
  ];

  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>[
            Expanded(
              child: ListView.builder(
                  padding: const EdgeInsets.all(8),
                  itemCount: _categories.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _categories[index];
                  }),
            ),
            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),
      ),
    );
  }
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

在集合视图中对不同类型的项目进行分组和显示 - Xamarin Forms

2022-9-8 19:37:21

未分类

在集合视图中对不同类型的项目进行分组和显示 - Xamarin Forms

2022-9-8 19:48:36

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索