Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SafeArea in every route?

Tags:

flutter

dart

Is it possible to set SafeArea once for every route? For now I have no idea how to do that nor found such question. Do I have to put it every screen view?

like image 680
nzxcvbnm Avatar asked Oct 20 '25 09:10

nzxcvbnm


2 Answers

You can copy paste run full code below
You can use builder of MaterialApp

code snippet

MaterialApp(
    title: 'Builder Demo',
    builder: (BuildContext context, Widget child) {
      return SafeArea(
        child: child,
      );
    },

full code

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Builder Demo',
    builder: (BuildContext context, Widget child) {
      return SafeArea(
        child: child,
      );
    },
    initialRoute: '/',
    routes: {
      '/': (context) => FirstScreen(),
      '/second': (context) => SecondScreen(),
    },
  ));
}

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Launch screen'),
          onPressed: () {
            // Navigate to the second screen using a named route.
            Navigator.pushNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // Navigate back to the first screen by popping the current route
            // off the stack.
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}
like image 125
chunhunghan Avatar answered Oct 23 '25 03:10

chunhunghan


I had to add scaffold widget as child of safearea because it complained about widget parameter being nullable...

builder: (context, widget) {
    return SafeArea(
      child: Scaffold(
        body: widget,
      ),
    );
  },
like image 45
Noob Avatar answered Oct 23 '25 02:10

Noob