flutter 基本路由跳转、传参、末尾浮动按钮组件FloatingActionButton

逃离我推掉我的手 2021-07-25 00:40 504阅读 0赞
  1. 路由就是组件即dart文件
  2. 1、路由跳转
  3. (1)在要跳转的dart文件中先导入被跳转的dart文件
  4. (2)在回调函数中
  5. (3)因为跳转的路由是单独的页面,所以没有主题样式,需要重新设置,即return配置Scaffold
  6. 跳转到指定路由:
  7. Navigator.of(context).push(
  8. MaterialPageRoute(
  9. builder: (context)=>组件名()
  10. )
  11. );
  12. 回退到上一个路由:
  13. Navigator.of(context).pop();
  14. 2、跳转传参数
  15. 无状态组件的路由
  16. (1)通过构造函数指定参数
  17. (2)在跳转的组件中:(context)=>组件名(传入参数)
  18. 有状态组件的路由
  19. (1)第一个类通过构造函数指定参数并接收
  20. (2)在第一个类中传入参数到第二个类
  21. (3)第二个类通过构造函数指定参数并接收,虽然两个类是继承,但这里不能使用this获取父类的属性
  22. 3、末尾浮动组件
  23. (1)该按钮组件固定位置在屏幕右下角
  24. (2)在Scaffold中设置与appBar同级
  25. floatingActionButton: FloatingActionButton(
  26. child:组件,
  27. tooltip:长按显示的内容,
  28. onPressed: (){
  29. ...
  30. },
  31. ),
  32. //设置按钮位置
  33. floatingActionButtonLocation:FloatingActionButtonLocation.endTop

代码示例:

路由待跳转文件

  1. import 'package:flutter/material.dart';
  2. //引入路由文件
  3. import 'me.dart';
  4. import 'search.dart';
  5. class Home3 extends StatefulWidget {
  6. Home3({ Key key}) : super(key: key);
  7. @override
  8. _Home3State createState() => _Home3State();
  9. }
  10. class _Home3State extends State<Home3> {
  11. @override
  12. Widget build(BuildContext context) {
  13. return Container(
  14. child:Column(
  15. crossAxisAlignment: CrossAxisAlignment.center,
  16. mainAxisAlignment: MainAxisAlignment.center,
  17. children: <Widget>[
  18. //按钮跳转
  19. RaisedButton(child: Text("跳到search"),
  20. color:Theme.of(context).accentColor,
  21. onPressed:(){
  22. //导航跳转
  23. Navigator.of(context).push(
  24. MaterialPageRoute(
  25. builder: (context)=>Search(title:'哈哈哈')
  26. )
  27. );
  28. }
  29. ),
  30. SizedBox(height: 20,),
  31. //按钮跳转
  32. RaisedButton(child: Text("跳到Me"),
  33. color:Theme.of(context).accentColor,
  34. onPressed:(){
  35. //导航回退
  36. Navigator.of(context).push(
  37. MaterialPageRoute(
  38. builder: (context)=>Me(title: "哇咔咔")
  39. )
  40. );
  41. }
  42. ),
  43. ],
  44. ),
  45. );
  46. }
  47. }

有状态路由页面:

  1. import 'package:flutter/material.dart';
  2. class Me extends StatefulWidget {
  3. //构造函数接收参数
  4. String title='me';
  5. Me({ this.title="表单"});
  6. @override
  7. //传递给第二个子类
  8. _Home2State createState() => _Home2State(title2:this.title);
  9. }
  10. class _Home2State extends State<Me> {
  11. //子类通过构造函数接收
  12. String title2;
  13. _Home2State({ this.title2='hh'});
  14. @override
  15. //这里虽然继承,但不能使用this获取父类的属性
  16. Widget build(BuildContext context) {
  17. return Scaffold(appBar: AppBar(title: Text(this.title2),),
  18. body: Home(),
  19. );
  20. }
  21. }
  22. class Home extends StatelessWidget {
  23. @override
  24. Widget build(BuildContext context) {
  25. return Container(
  26. child: Text("meeee"),
  27. );
  28. }
  29. }
  30. /* 单独的页面没有主题样式,需要通过Scaffold自行设置 */

无状态路由页面:

  1. import 'package:flutter/material.dart';
  2. class Search extends StatelessWidget {
  3. String title;
  4. //构造函数接收参数
  5. Search({ this.title='SEARCCC'});
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. //浮动按钮
  10. floatingActionButton: FloatingActionButton(
  11. child: Text('返回'),
  12. onPressed: (){
  13. //导航返回
  14. Navigator.of(context).pop();
  15. },
  16. ),
  17. appBar: AppBar(title:Text(this.title)),
  18. body:Home()
  19. );
  20. }
  21. }
  22. class Home extends StatelessWidget {
  23. @override
  24. Widget build(BuildContext context) {
  25. return Container(
  26. child: Text("searcheeee1"),
  27. );
  28. }
  29. }

发表评论

表情:
评论列表 (有 0 条评论,504人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue+

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat