gpt4 book ai didi

android - 为什么 ListView 不显示用户输入的文本

转载 作者:行者123 更新时间:2023-12-04 14:52:48 25 4
gpt4 key购买 nike

我需要你的帮助。

我正在为我的应用程序创建一个功能,让用户通过按添加按钮添加一些东西,然后它会导航到一个添加页面,然后从添加页面它会在 ListView 中添加一个新的 listtile。但是我不知道为什么不能显示用户输入的文本。谁能帮帮我?

import 'package:flutter/material.dart';
import 'storage for each listview.dart';
import 'package:provider/provider.dart';
import 'adding page.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => Storage(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage()
),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final provider = Provider.of<Storage>(context, listen: false);
final storageaccess = provider.storage;
return Scaffold(
appBar: AppBar(
title: Text('app'),
),
body: ListView.builder(
itemCount: storageaccess.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(storageaccess[index].title),
subtitle: Text(storageaccess[index].titlediary.toString()),
onTap: () {},
onLongPress: () {
//delete function here
},
);
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => addpage()));
}, //void add
tooltip: 'add diary',
child: Icon(Icons.add),
) // This trailing comma makes auto-formatting nicer for build methods.
);
}
}



/// this one I did not do anything first this one for later today just make UI
class Things {
String title;
DateTime titlediary;

Things({required this.title, required this.titlediary});
}

class addpage extends StatefulWidget {
@override
_addpageState createState() => _addpageState();
}

class _addpageState extends State<addpage> {

String title = '';

@override
Widget build(BuildContext context) {
final TextEditingController titleController=TextEditingController(text: title);
final formKey = GlobalKey<FormState>();
return Scaffold(
appBar: AppBar(
title: Text('enter page ',style: TextStyle(fontSize: 30),),
),
body:Form(
key: formKey,
child: Column(
children: [
TextFormField(
controller: titleController,
autofocus: true,
validator: (title) {
if (title!.length < 0) {
return 'enter a title ';
} else {
return null;
}
},
decoration: InputDecoration(
border: UnderlineInputBorder(),
labelText: 'title',
),
),
SizedBox(height: 8),
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.black),
),
onPressed: () {
if (formKey.currentState!.validate()) {
final accessthing = Things(
title: title,
titlediary: DateTime.now(),
);
final provideraccess = Provider.of<Storage>(context, listen: false);
provideraccess.add(accessthing);
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>MyHomePage()));
}
},
child: Text('Save'),
),
],
),),);
}
}

class Storage extends ChangeNotifier {
List<Things> storage = [
Things(
title: 'hard code one ',
titlediary: DateTime.now(),
),
Things(
title: 'hard code two ',
titlediary: DateTime.now(),
),
Things(
title: 'hard code two ',
titlediary: DateTime.now(),
),
Things(
title: 'hard code two ',
titlediary: DateTime.now(),
),
];
void add(Things variablethings) {
storage.add(variablethings);
} notifyListeners();
}

用户点击add按钮后,会跳转到adding页面,点击save后,数据会被保存到storage页面,然后provider会添加用户提供的数据,但是text会不显示在 listtile 上。

最佳答案

我怀疑发生这种情况是因为您再次使用导航到主页,

Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>MyHomePage()));

但这次它没有连接到您的提供者上下文。

[详细说明:因为您在 MyApp 中使用了 ChangeNotifierProvider() 然后在那里连接了 MyHomePage()。但是,如果您在 Navigator 中再次按下,则 flutter 会创建一个单独的 MyHomePage() 小部件实例。它将不会连接到 MyApp 中的 ChangeNotifierProvider()].

代替这个,使用Navigator.of(context).pop();在 onPressed() 中使用它,

floatingActionButton: FloatingActionButton(
---> onPressed: () async {
---> await Navigator.push(
context, MaterialPageRoute(builder: (context) => addpage()));
---> setState((){});
},

关于android - 为什么 ListView 不显示用户输入的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68769711/

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