02 fluter学习笔记 - xiaoxin01/Blog GitHub Wiki

定义方法,参数默认值、参数必须

TapboxB({Key key, this.active: false, @required this.onChanged})

传递方法

void _handleTapboxChanged(bool newValue) {}

final ValueChanged<bool> onChanged = _handleTapboxChanged;

路由

MaterialApp(
  // Start the app with the "/" named route. In this case, the app starts
  // on the FirstScreen widget.

  // 使用“/”命名路由来启动应用(Start the app with the "/" named route. In our case, the app will start)
  // 在这里,应用将从 FirstScreen Widget 启动(on the FirstScreen Widget)

  initialRoute: '/',
  routes: {
    // When navigating to the "/" route, build the FirstScreen widget.
    // 当我们跳转到“/”时,构建 FirstScreen Widget(When we navigate to the "/" route, build the FirstScreen Widget)
    '/': (context) => FirstScreen(),
    // When navigating to the "/second" route, build the SecondScreen widget.
    // 当我们跳转到“/second”时,构建 SecondScreen Widget(When we navigate to the "/second" route, build the SecondScreen Widget)
    '/second': (context) => SecondScreen(),
  },
);

onPressed: () {
  // Navigate to the second screen using a named route.
  // 使用命名路由跳转到第二个界面(Navigate to the second screen using a named route)
  Navigator.pushNamed(context, '/second');
}

路由传递参数

  1. 提取参数
class ScreenArguments {
  final String title;
  final String message;

  ScreenArguments(this.title, this.message);
}

Navigator.pushNamed(
  context,
  ExtractArgumentsScreen.routeName,
  arguments: ScreenArguments(
    'Extract Arguments Screen',
    'This message is extracted in the build method.',
  ),
);

class ExtractArgumentsScreen extends StatelessWidget {
  static const routeName = '/extractArguments';

  @override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute settings and cast
    // them as ScreenArguments.
    final ScreenArguments args = ModalRoute.of(context).settings.arguments;
  }
}
  1. 传递参数
onGenerateRoute: (settings) {
  // If you push the PassArguments route
  if (settings.name == PassArgumentsScreen.routeName) {
    // Cast the arguments to the correct type: ScreenArguments.
    final ScreenArguments args = settings.arguments;

    // Then, extract the required data from the arguments and
    // pass the data to the correct screen.
    return MaterialPageRoute(
      builder: (context) {
        return PassArgumentsScreen(
          title: args.title,
          message: args.message,
        );
      },
    );
  }
},

navigate-with-arguments

从一个页面返回数据

Navigator.pop() 方法可以接受第二个参数 result,它是可选的,如果传递了 result,数据将会通过 Future 方法的返回值传递。

Navigator.pop(context, 'Yep!');
...

final result = await Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => SelectionScreen()),
);
// 等选择界面返回结果,先隐藏之前的 snackbars,结果显示在新的 snackbars 里  (After the Selection Screen returns a result, hide any previous snackbars and show the new result!)
Scaffold.of(context)
  ..removeCurrentSnackBar()
  ..showSnackBar(SnackBar(content: Text("$result")));
⚠️ **GitHub.com Fallback** ⚠️