首页 > 解决方案 > 匹配导航器小部件中的子高度大小

问题描述

我的小部件有问题Navigator:我需要匹配子小部件 height

Navigator的小部件在一个小部件内,Expanded但是当我将路由更改为具有不同大小的子小部件时,它不起作用:Navigator大小相同......

这是一个更清晰和快速测试的完整示例:)

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

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

class MyApp extends StatelessWidget {
  final GlobalKey<NavigatorState> _navigationKey = GlobalKey();

  MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: [
            Container(
              color: Colors.blue,
              height: 100,
            ),
            Flexible(
              // When I set a SizedBox here, Navigator fit the size
              // But I want an "automatic" size: the widget child size
              child: Navigator(
                key: _navigationKey,
                initialRoute: 'test',
                onGenerateRoute: (settings) {
                  if (settings.name == 'test') {
                    return MaterialPageRoute(
                      builder: (context) {
                        return RaisedButton(
                          onPressed: () {
                            Navigator.pushNamed(
                              _navigationKey.currentContext,
                              'tesdf',
                            );
                          },
                          // I just create a Text widget on this route here
                          // so I want the Navigator to fit the Text height size
                          // when displaying this route 
                          child: Text('rjkdf'),
                        );
                      },
                    );
                  } else {
                    return MaterialPageRoute(
                      builder: (context) {
                        // I set the size of the children to 80 but
                        // it seems to be ignored :/
                        // the Navigator fill the entire size but
                        // I want to be 80 when displaying this route 
                        return Container(
                          height: 80,
                          color: Colors.pink,
                        );
                      },
                    );
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

有人遇到过这个问题吗?提前致谢!

标签: flutterdartflutter-layoutflutter-navigation

解决方案


推荐阅读