flutter 路由與導(dǎo)航

參考鏈接

1. 簡(jiǎn)單的導(dǎo)航跳轉(zhuǎn)
  • 第一個(gè)頁(yè)面
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(),
            ));
          },
        ),
      ),
    );
  }
}
  • 第二個(gè)頁(yè)面
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);
          },
        ),
      ),
    );
  }
}
  • main
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstScreen(),
    );
  }
}
列表跳轉(zhuǎn)的小例子
  • 文章的模型
// 文章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 : 離離原上草,一生一世枯榮. 野火燒不盡,春風(fēng)吹又生.",
          ),
  );

  @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),
                ),
              );
            }
          },
        );
      },
    );
  }
}
  • 內(nèi)容界面
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'),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
  • 實(shí)現(xiàn)
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(),
      ),
    );
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容