我有一个添加表单页面,并按下了add form
按钮,我希望它向列表类List<Crime> crimes;
中添加一个新项目,其标题,描述和位置。然后在带有list_view的页面中,我要访问此列表,并使用输入的信息列出每个小部件的列表。
下面是类Crime
:
//crime_model.dart
class Crime {
String title;
String location;
String description;
Crime({this.title,this.description,this.location});
}
这是“添加帖子”页面-(已删除了不必要的代码)
//add_post_page.dart
import 'package:flutter/material.dart';
import 'profile_page.dart';
import 'home.dart';
import 'package:maps/models/crime.dart';
import 'package:maps/posts_list.dart';
import 'post_lists_improved.dart' as list_view;
class AddPostScaffold extends StatefulWidget{
State createState() => _AddPostState();
}
class _AddPostState extends State<AddPostScaffold>{
int _currentIndex = 0;
final titleController = TextEditingController();
final descriptionController = TextEditingController();
String location = "Edmonton, UK";
body: Padding(
padding: EdgeInsets.fromLTRB(8,85,8,0),
child:Column(
mainAxisSize : MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(15 , 15,15,0),
child: Column(children: <Widget>[
new Container (
child: new Theme(
data: new ThemeData(
primaryColor: Colors.red,
primaryColorDark: Colors.redAccent,
),
child: Padding(
padding: EdgeInsets.fromLTRB(25,25,25,25),
child: new TextField(
controller: titleController,
maxLines: 1,
maxLength: 20,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.black)),
hintText: 'Type of crime',
helperText: 'e.g : Murder , Theft , Robbery , Vehicular Crime',
labelText: 'Type of crime',
prefixIcon: const Icon(
Icons.donut_large,
color: Colors.redAccent,
),
),
))
)
)
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(15,0,15,15),
child: Column(children: <Widget>[
new Container (
child: new Theme(
data: new ThemeData(
primaryColor: Colors.red,
primaryColorDark: Colors.redAccent,
),
child: Padding(
padding: EdgeInsets.all(25),
child: new TextField(
controller: descriptionController,
maxLines: null,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.black)),
hintText: 'Description',
helperText: 'SHORT SUMMARY',
labelText: 'Description',
prefixIcon: const Icon(
Icons.info,
color: Colors.redAccent,
),
),
))
)
)
],
),
),
Padding(
padding: const EdgeInsets.all(15),
child: Center(child:Container(
width: 300,
height: 75,
child: FlatButton(
child: const Text("Post Crime", style: TextStyle(color: Colors.white , fontSize: 20)),
和send data
是应该使用send data
函数将项目添加到类中的地方
onPressed: ()async{
sendData(location,titleController.text,descriptionController.text);
Navigator.pop(context);
},
),
decoration: BoxDecoration(
color : Colors.black,
borderRadius: BorderRadius.all(Radius.circular(25)),
border: Border.all(color: Colors.black , style: BorderStyle.solid)
),),
))
],
)
),);
}
}
下面是小部件列表将位于的页面
//posts_list.dart
import 'package:flutter/material.dart';
import 'add_post.dart';
import 'models/crime.dart';
class ViewPosts extends StatefulWidget {
State createState() => _ViewPostsState();
}
class _ViewPostsState extends State<ViewPosts> {
List<Crime> crimes = List();
sendData(location,title,description)async{
setState(() {
crimes.add(Crime(title: title, location: location, description: description));
});
}
@override
Widget build(BuildContext context) {
}
}
我尝试将posts list
页面作为add posts
导入到import 'post_list.dart as list_view;
页面中,并在所需位置将函数与list_view.ViewPosts.sendData(...)
一起使用,但是它显示sendData isn't defined for class ViewPosts
谢谢