【发布时间】:2021-07-17 12:22:36
【问题描述】:
这是一个非常基本的应用程序,它在点击时突出显示选定的颜色。但我希望将图标引导到列表视图。我怎样才能做到这一点?如果我在小部件中添加一个图标,则相同的图标会在任何地方呈现。我想要每个列表的唯一图标。请帮忙。这是我的代码:
我想为每个列表渲染图标。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
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> {
List<String> texts = ['ME', 'MYSELF', 'I'];
List<bool> isHighlighted = [true, false, false];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo App'),
),
drawer: Drawer(
child: Center(
child: Column(children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: texts.length,
itemBuilder: (_, index) {
return GestureDetector(
onTap: () {
for (int i = 0; i < isHighlighted.length; i++) {
setState(() {
if (index == i) {
isHighlighted[index] = true;
} else {
//the condition to change the highlighted item
isHighlighted[i] = false;
}
});
}
},
child: Container(
color: isHighlighted[index]
? Colors.blueAccent
: Colors.white,
child: ListTile(
//i want to display different items for each list in the leading property.
title: Text(texts[index]),
),
),
);
}),
),
Container(
child: Text(
'this is footer',
style: TextStyle(fontSize: 20),
),
)
]),
),
),
);
}
}
【问题讨论】:
-
请标记您的编程语言
-
完成。谢谢。
标签: flutter dart flutter-layout dart-pub flutter-listview