gpt4 book ai didi

dart - Flutter 自定义图标不呈现

转载 作者:IT王子 更新时间:2023-10-29 06:43:54 26 4
gpt4 key购买 nike

我正在尝试使用以下文章将自定义图标添加到抽屉导航: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);
}


}

请帮助我解决这个问题。提前致谢

最佳答案

此导入无效:

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 中)

关于dart - Flutter 自定义图标不呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52792630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com