gpt4 book ai didi

flutter - 如何将 Onpressed 函数添加到列表的每个值

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

我在将列表中的项目导航到新的 .dart 文件时遇到了困难,这是我导入第二个文件的主页

class Home extends StatefulWidget {
@override
_HomeState createState() => new _HomeState();
}

class _HomeState extends State<Home> {

final List<String>listof=["Decimal Numbers","Binary Numbers","Decimal-to-Binary Conversion","Binary Arithmetic","Complements of Binary Numbers","Signed Numbers","Arithmetic Operations with Signed Numbers","Hexadecimal Numbers","Octal Numbers","Binary Coded Decimal (BCD)"];



@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(
"Computer System Theory",
style: new TextStyle(fontSize: 19.0),
),
backgroundColor: Colors.deepPurple,

actions: <Widget>[
new IconButton(
icon: new Icon(Icons.search),
onPressed: ()=>debugPrint("Search"),
),
],

),
body: new Container(
child: new ListView.builder(

itemBuilder: (_, int index)=>listDataItem(this.listof[index],),
itemCount: this.listof.length,


),
),
);
}
}

class listDataItem extends StatelessWidget{

String itemName;
listDataItem(this.itemName);

@override
Widget build(BuildContext context){
return new Card(

elevation: 7.0,

child: new Container(

margin: EdgeInsets.all(5.0),
padding: EdgeInsets.all(6.0),

child: new Row(
children: <Widget> [

new CircleAvatar(
child: new Text(itemName[0]),

backgroundColor: Colors.deepPurple,
foregroundColor: Colors.white,

),
new Padding(padding: EdgeInsets.all(8.0)),
new Text(itemName,style: TextStyle(fontSize: 18.0)),

],
),
),);
}

}

我尝试了这种方法,但我不知道如何将 onpress 函数添加到列表中的一个 iten,如果我单击列表中的任何值,应该会带我到相关页面,例如 binary.dart

这是我的二进制代码

import 'package:flutter/material.dart';

class Binary extends StatefulWidget {
@override
_BinaryState createState() => _BinaryState();
}

class _BinaryState extends State<Binary> {
@override
Widget build(BuildContext context) {
double cWidth = MediaQuery.of(context).size.width*0.8;
return new Container (
padding: const EdgeInsets.all(16.0),
width: cWidth,
child: new Column (
children: <Widget>[
new Text ("Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 ", textAlign: TextAlign.left),
new Text ("Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2", textAlign: TextAlign.left),
],
),
);
}
}

最佳答案

您可以使用 InkWellGestureDetector 包装您的列表项。

示例:在您的 listDataItem 类的 build 方法中,您将返回一个 Card

用墨水瓶把它包起来。

InkWell(
onTap: () {
print('inkwell');
},
child: Card(...

关于flutter - 如何将 Onpressed 函数添加到列表的每个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55817080/

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