参考链接
1. 简单的导航跳转
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("FirstScreen"),),
body: Center(
child: RaisedButton(
child: Text("Launch Second Screen"),
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => SecondScreen(),
));
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Second Screen"),),
body: Center(
child: RaisedButton(
child: Text("Back"),
onPressed: () {
Navigator.pop(context);
},
),
),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstScreen(),
);
}
}
列表跳转的小例子
// 文章model
class Article {
String title;
String content;
Article({this.title,this.content});
}
// 文章列表
class ArticleListScreen extends StatelessWidget {
final List<Article> articles = List.generate(
10,
(index) => Article(
title: "Article $index",
content: "Article $index : 离离原上草,一生一世枯荣. 野火烧不尽,春风吹又生.",
),
);
@override
Widget build(BuildContext context) {
return ListView.separated(
itemCount: articles.length,
separatorBuilder: (context,index) {
return Divider(height: 0.5,color: Colors.grey,);
},
itemBuilder: (context,index) {
return ListTile(
title: Text(articles[index].title),
onTap: () async {
String result = await Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ContentScreen(article: articles[index],),
),
);
if (result != null) {
Scaffold.of(context).showSnackBar(
new SnackBar(
content: new Text("$result"),
duration: const Duration(seconds: 1),
),
);
}
},
);
},
);
}
}
class ContentScreen extends StatelessWidget {
final Article article;
ContentScreen({this.article});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("${article.title}"),
),
body: Padding(
padding: new EdgeInsets.all(15.0),
child: new Column(
children: <Widget>[
new Text('${article.content}'),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new RaisedButton(
onPressed: () {
Navigator.pop(context, 'Like');
},
child: new Text('Like'),
),
new RaisedButton(
onPressed: () {
Navigator.pop(context, 'Unlike');
},
child: new Text('Unlike'),
),
],
)
],
),
),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Articles List"),),
body: ArticleListScreen(),
),
);
}
}