6

I have the following widget tree:

@override
  Widget build(BuildContext context) {
    final double topMargin = Platform.isAndroid ? 0 : 105; // TODO: (why margin on iOS?)
    final double interMargin = Platform.isAndroid ? 0 : 10;
    final body = Column(children: <Widget> [
      Padding(
        padding: EdgeInsets.only(left: 10, right: 10, top: topMargin),
        child: Platform.isAndroid // url
        ? TextField(
            decoration: new InputDecoration(hintText: 'Host'),
            maxLines: 1,
            autofocus: true,
            textInputAction: TextInputAction.next,
            controller: _hostController)
        : CupertinoTextField(
            maxLines: 1,
            autofocus: true,
            textInputAction: TextInputAction.next,
            controller: _hostController)),
      Padding(
        padding: EdgeInsets.only(left: 10, top: interMargin, right: 10),
        child: Platform.isAndroid // port
          ? TextField(
              decoration: new InputDecoration(hintText: 'Port'),
              keyboardType: TextInputType.number,
              maxLines: 1,
              controller: _portController)
          : CupertinoTextField(
              keyboardType: TextInputType.number,
              maxLines: 1,
              controller: _portController)),
      Platform.isAndroid
        ? RaisedButton(child: Text('OK'), onPressed: () => _onInputFinished())
        : CupertinoButton(child: Text('OK'), onPressed: () => _onInputFinished())
    ]);
    return Platform.isAndroid
      ? Scaffold(
          appBar: AppBar(title: Text('Server connection')),
          body: body)
      : CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(middle: Text('Server connection')),
          child: body);
  }

It looks ok only if i add top margin to body widgets:

final double topMargin = Platform.isAndroid ? 0 : 105; // TODO: (why margin on iOS?)
...

iOS screenshot

If i don't add it CupertinoNavigationBar overlaps children:

overlapping

What am i missing?

Here is the whole project and the screen.

4ntoine
  • 19,816
  • 21
  • 96
  • 220

2 Answers2

13

You just need to implement 'SafeArea' widget (instead of top margin) to your children's widgets to avoid being overlapped by the Cupertino Navigation bar.

CupertinoPageScaffold(
   navigationBar: CupertinoNavigationBar(),
   child: SafeArea(
            child: [Your widgets] // your children widgets 
        ),
)
Alvindrakes
  • 596
  • 7
  • 18
  • 1
    According to the [doc](https://api.flutter.dev/flutter/widgets/SafeArea-class.html): "A widget that insets its child by sufficient padding to avoid intrusions by the operating system". In my case it's expected that the area is occupied by another widget (`CuperinoNavigationBar` and it's intentional). So using of `SafeArea` looks like a workaround. Any thoughts on this? Any usage of `SafeArea` in official docs/sources for this use case? – 4ntoine Dec 18 '19 at 07:10
7

You have to set barBackgroundColor for CupertinoApp. Or setting a backgroundColor for CupertinoNavigationBar will also solve this problem.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      title: 'Flutter App',
      theme: CupertinoThemeData(
        barBackgroundColor: CupertinoColors.white,
      ),
      home: MyHomePage(title: 'Flutter App'),
    );
  }
}

or

CupertinoPageScaffold(
  navigationBar: CupertinoNavigationBar(
    middle: Text('MyStackPageState'),
    backgroundColor: CupertinoColors.white,
  ),
  child: Container(),
}
Cheng Lei
  • 71
  • 1
  • 2