颤动 - 不同视图之间的路由

时间:2017-04-28 14:57:29

标签: view routing dart flutter

所以,我有这个文件:

import 'package:flutter/material.dart';
import "calculateDerivations.dart";
import "calculateRoots.dart";

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Ableitungen berechnen'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: new AppBar(title: new Text(config.title)),
      body: new Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          new InputWidget(),
        ]
      ),
    );
  }
}

class InputWidget extends StatefulWidget {
  @override
  InputWidgetState createState() => new InputWidgetState();
}

class InputWidgetState extends State<InputWidget> {

  InputValue val = new InputValue(text: "");
  String ersteAbleitung = "";
  String zweiteAbleitung = "";
  String dritteAbleitung = "";
  String roots = "";
  String function = "";

  void _submitted(){
    setState((){
      /*
       * Redirect here
       */
    });
  }

  @override
  Widget build(BuildContext context) {

    return new Column(
        children: [
          new Input(
            value: val,
            labelText: 'Funktion hier eingeben',
            onChanged: (InputValue newInputValue) {
              setState(() {
                val = newInputValue;
              });
          }),
          new IconButton(
            icon: new Icon(Icons.check),
            onPressed: _submitted,
          )
        ]
    );
  }
}

用户现在单击IconButton(调用_submitted),我希望他被重定向到新的View(Widget)。我如何在Flutter中解决这个路由问题?

提前致谢

1 个答案:

答案 0 :(得分:8)

正常路线导航可能如下所示:

new IconButton(
    icon: new Icon(Icons.check),
    onPressed: () {
        Navigator.push(context, new MaterialPageRoute(
            builder: (_) => new MyCustomView(),
        );
    )
)

您还可以通过传递WidgetBuilder s的地图作为routes的{​​{1}}构造函数参数,或者传递MaterialApp处理程序来使用命名路由。在Flutter画廊中有example个命名路线。

如果您不想要动画,请参阅我对this question的回答。