为啥列表视图不显示用户输入的文本
Posted
技术标签:
【中文标题】为啥列表视图不显示用户输入的文本【英文标题】:why isn't the listview showing the text that input by the user为什么列表视图不显示用户输入的文本 【发布时间】:2021-08-13 09:15:02 【问题描述】:我需要你的帮助。
我正在为我的应用程序创建一个功能,让用户通过按添加按钮添加一些内容,然后它将导航到添加页面,然后从添加页面它将在列表视图中添加一个新的列表图块。但我不知道为什么无法显示用户输入的文本。谁能帮帮我?
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();
用户点击添加按钮后,会发送到添加页面,点击保存后,数据会保存到存储页面,然后提供者会添加用户提供的数据,但文本会不显示在 listtile 上。
【问题讨论】:
【参考方案1】:我怀疑这是因为您再次使用导航到主页,
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>MyHomePage()));
但这次它没有连接到您的提供者上下文。
[
详细说明:因为您在MyApp
中使用了ChangeNotifierProvider()
,然后在那里连接了MyHomePage()
。但如果你在 Navigator 中再次推送,fluter 会创建一个单独的 MyHomePage()
小部件实例。在MyApp
中不会连接到ChangeNotifierProvider()
].
代替这个,使用Navigator.of(context).pop();
而在 onPressed() 中使用这个,
floatingActionButton: FloatingActionButton(
---> onPressed: () async
---> await Navigator.push(
context, MaterialPageRoute(builder: (context) => addpage()));
---> setState(());
,
【讨论】:
以上是关于为啥列表视图不显示用户输入的文本的主要内容,如果未能解决你的问题,请参考以下文章
为啥输入的文本或字母没有显示在 Iphone 模拟器的 alertView 文本字段中?