这个小部件的布局?

时间:2019-01-09 04:51:27

标签: flutter

我正在尝试Flutter。我想构建一个具有三个屏幕的应用程序:加载屏幕,注册屏幕,仪表板。注册屏幕具有单独的Navigator和3个由导航器实例处理的子屏幕。我已经实现了大多数功能,但到目前为止,找不到一个简单问题的解决方案:注册屏幕上显示的页码与当前显示的子屏幕相对应。在前进导航到新的子屏幕时,它已正确更新,但在返回时却未正确更新。这是在注册屏幕本身中唯一更新的元素,除了小部件的子屏幕树以外。到目前为止,在注册屏幕和子屏幕中都有我的代码:

import 'package:flutter/material.dart';

class RegisterScreenState extends State<RegisterScreen> {
  int _page;
  final GlobalKey<NavigatorState> _nav = GlobalKey();
  @override
  void initState() {
    super.initState();
    this._page = widget.page;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => !await _nav.currentState.maybePop(),
      child: Scaffold(
        body: SafeArea(
          child: Container(
            margin: EdgeInsets.all(20),
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    CircleAvatar(
                      backgroundColor: Colors.orange,
                      radius: 12.5,
                      child: Text((_page + 1).toString()),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 10),
                      child: Text(widget.isInRegisterMode
                          ? 'Register'
                          : 'User info'),
                    ),
                  ],
                ),
                Expanded(
                  child: Navigator(
                    key: _nav,
                    initialRoute: widget.page.toString(),
                    onGenerateRoute: (RouteSettings settings) {
                      WidgetBuilder builder;
                      int p = int.parse(settings.name);
                      if (p != _page)
                        setState(() {
                          _page = p;
                        });
                      switch (p) {
                        case 0:
                          builder = (_) => RegisterForm();
                          break;
                        case 1:
                          builder =
                              (_) => Text('UserInfoForm'); //placeholder;
                          break;
                        default:
                          throw Exception('Invalid route: ${settings.name}');
                      }
                      return MaterialPageRoute(
                          builder: builder, settings: settings);
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class RegisterScreen extends StatefulWidget {
  final int page;
  final bool isInRegisterMode;
  RegisterScreen(this.page, this.isInRegisterMode) : super();
  @override
  RegisterScreenState createState() => RegisterScreenState();
}

class RegisterForm extends StatelessWidget {
  @override
  Widget build(BuildContext ctx) {
    return Row(children: [
      Text('RegisterForm'),
      RaisedButton(
        child: Text('OK'),
        onPressed: () => Navigator.of(ctx).pushNamed('1'),
      ),
    ]);
  }
}

0 个答案:

没有答案