Flutter自定义图标未渲染

时间:2018-10-13 11:52:08

标签: dart flutter

我正在尝试使用以下文章来将自定义图标添加到导航抽屉:https://medium.com/flutterpub/how-to-use-custom-icons-in-flutter-834a079d977。但是,图标未呈现。代码如下:

import 'package:flutter/material.dart';
import '../custom_app_icons.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return new MaterialApp(
    title: 'Flutter Demo',
    theme: new ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: new Scaffold(
      appBar: new AppBar(
        title: new Text("check"),
      ),
      drawer: XmobeMenu(5),
    ),

  );
}
}

final List<MenuItem> menuItems = <MenuItem>[
MenuItem(0,'Home',custom.home,Icons.chevron_right),
MenuItem(0,'Home',custom.home,Icons.chevron_right),
MenuItem(0,'Home',Icons.home,Icons.chevron_right),
];

class XmobeMenu extends StatelessWidget {
int indexNumber;
XmobeMenu(int menuIndex)
{
  indexNumber =menuIndex;
}
@override
Widget build(BuildContext context) {
  return Drawer(
    child: ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return MenuItemWidget(menuItems[index],indexNumber);
      },
      itemCount: menuItems.length,
    ),
  );
}
}

class MenuItem {
MenuItem(this.itemNumber,this.title, this.leadIcon, this.trailIcon,);
final int itemNumber;
final IconData leadIcon;
final IconData trailIcon;
final String title;
}

class MenuItemWidget extends StatelessWidget {
final MenuItem item;
final int indexNumber;
const MenuItemWidget(this.item, this.indexNumber);

Widget _buildMenu(MenuItem menuItem, context) {
  return InkWell(
    onTap: () {
      Navigator.of(context).push(
        new MaterialPageRoute(
          builder: (BuildContext context) => MyApp(),
        ),
      );
    },
    child: new Container(
      color: const Color.fromARGB(0, 245,245,245),
      child: new Column(
        children: <Widget>[
          new Column( children: <Widget>[
            Container(
                padding: new EdgeInsets.all(8.0), 
                child: Row(
                  children: <Widget>[
                    new Icon(menuItem.leadIcon),
                    new Expanded (
                      child: new Text(menuItem.title),
                    ),
                    new Icon(menuItem.trailIcon),
                  ],
                )
            ),
            Divider(height: 1.0,color: Colors.grey,),
          ],)
        ],
      ),

    ),
  );
}

@override
Widget build(BuildContext context) {
  return _buildMenu(this.item, context);
}


}

请协助我解决此问题。预先感谢

1 个答案:

答案 0 :(得分:1)

此导入无效:

import '../custom_app_icons.dart';

从不使用相对路径导航到lib/之外的文件

当前还有一个错误(已在Dart中修复,但尚未在Flutter中降落)会在lib/main.dart中使用相对路径时引起问题。

要解决此问题,请将custom_app_icons.dart移动到lib/下方的某个位置,然后像导入

import 'package:my_app/icons/custom_app_icons.dart';

(假设custom_app_icons.dartlib/icons/custom_app_icons.dart中)