我不熟悉Flutter开发,作为学习项目的一部分,我正在开发一个简单的记事本应用程序。有什么方法可以从另一个小部件类(方法调用等)重建小部件。
我正在应用中使用2个小部件。无状态和有状态的小部件。
我使用“ saveidea”小部件将注释保存到数据库,并且数据库中的元素在“ Showidea”小部件中显示为列表视图。 每当我向数据库添加注释时,我都希望下面的列表视图立即更新。 每当我单击“ Saveidea”小部件中的“保存”时,是否可以更新“ Showidea”列表视图?
谢谢。
其他信息:
代码“ SaveIdea窗口小部件”
class SaveIdea extends StatelessWidget {
DatabaseHelper _databaseHelper = DatabaseHelper();
final TextEditingController mTitleText = new TextEditingController();
final TextEditingController mContentText = new TextEditingController();
final DateFormat dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
String mTitle = "";
String mText = "";
@override
Widget build(BuildContext context) {
return Container(
child: Card(
color: Colors.blue[100],
child: Padding(
padding: EdgeInsets.only(right: 8.0, left: 4.0),
child: Column(
children: <Widget>[
TextField(
controller: mTitleText,
style: TextStyle(height: 2.0),
decoration: InputDecoration(hintText: 'Title'),
),
TextField(
controller: mContentText,
//..textfield code ),
ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('SAVE'),
onPressed: () {
savetext();
},
),
FlatButton(
child: const Text('CLEAR'),
onPressed: () {
cleartext();
},
),
],
),
],
),
),
),
);
}
savetext() async {
mTitle = mTitleText.text;
mText = mContentText.text;
if (mTitle == "" || mTitle == "Need Title !!!!!") {
mTitleText.text = "Need Title !!!!!";
return;
} else {
mTitleText.clear();
mContentText.clear();
int mid = await _databaseHelper.getCount();
mid++;
String mdate = dateFormat.format(DateTime.now());
int i = await _databaseHelper.insertIdea(
Idea(id: mid, iTitle: mTitle, iText: mText, date: mdate, status: 0));
}
}
代码:Showideas窗口小部件代码。
class Showideas extends StatefulWidget {
@override
_ShowideasState createState() => _ShowideasState();
}
class _ShowideasState extends State<Showideas> {
DatabaseHelper _databaseHelper = new DatabaseHelper();
List<Idea> listideas;
@override
Widget build(BuildContext context) {
if (listideas == null) {
updatelistideas();
}
return Container(
//.... code of listview.
);
}
void updatelistideas() async {
final ideas = await _databaseHelper.getIdeaList();
setState(() {
listideas = ideas;
});
int i = listideas.length;
debugPrint('ideaslength: $i listideaslength');
}
}