gpt4 book ai didi

listview - 使用显示对话框和文本字段更新 Flutter 中的单个 ListView 项

转载 作者:行者123 更新时间:2023-12-03 02:49:24 25 4
gpt4 key购买 nike

我们已经创建了一个 flutter 应用程序,它从一个项目列表中显示一个 ListView 。我们希望能够根据用户偏好更新这些。有点像如果您有一个待办事项列表并希望使用模式或对话框更正或更新列表中的一项。

我一直在尝试使用用户输入的对话框更新 ListView 中的单个项目,然后在提交时关闭。到目前为止,我没有运气,但我觉得我很接近。如何获取磁贴上的名称以在提交和重新呈现页面时更新?我错过了什么?

import 'package:flutter/material.dart';

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<Animal> animals = new List();
String newName;
TextEditingController nctrl = TextEditingController();

@override
void initState() {
super.initState();
animals = [
Animal(id: 1, name: 'cat'),
Animal(id: 2, name: 'dog'),
Animal(id: 3, name: 'mouse'),
Animal(id: 4, name: 'horse'),
Animal(id: 5, name: 'frog'),
];
}

_changePetName() {
newName = nctrl.text;
Navigator.pop(context);
return newName;
}

_showDialog(String name) {
nctrl.text = name;
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
elevation: 5,
backgroundColor: Colors.blue,
title: Text(
"Rename this pet",
),
content: Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: nctrl,
onChanged: (e) {
setState(() {});
},
),
],
)),
actions: <Widget>[
// usually buttons at the bottom of the dialog
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
color: Colors.green,
child: Text(
"Submit",
),
onPressed: () {
var updateName = _changePetName();
return updateName;
},
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: animals.length,
itemBuilder: (BuildContext context, int index) {
var pet = animals[index];
return ListTile(
key: ValueKey(pet.id),
enabled: true,
onTap: () async {
var rename = await _showDialog(pet.name);

if (rename != null) {
pet.name = rename;
setState(() {});
}
// setState(() {
// pet.name = 'bob';
// });
},
title: Text(pet.name),
);
},
),
],
),
),
);
}
}

class Animal {
final int id;
String name;

Animal({this.id, this.name});

factory Animal.fromJson(Map<dynamic, dynamic> json) {
return new Animal(
id: json['id'],
name: json['name'],
);
}

Map<dynamic, dynamic> toJson() {
final Map<dynamic, dynamic> data = new Map<dynamic, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
return data;
}
}

最佳答案

在 Onpress Method of Alert 对话框中,只需粘贴此代码

Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (_) => MyHomePage()));

我们会将您的页面导航到相同的页面。

关于listview - 使用显示对话框和文本字段更新 Flutter 中的单个 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59385363/

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