Search code examples
flutterviewroutesnavigator

Flutter navigator hides (covers) first window conent


Why when I use the navigator to go to another page(widget) that covers just part of the screen, I can't see the first-page content (which is on top of the page)?

enter image description here

enter image description here

I tried code from this example (https://docs.flutter.dev/cookbook/navigation/navigation-basics) and modified it a little to show what I need:

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  const FirstRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Route'),
      ),
      body: Center(
        child: ElevatedButton(
          child: const Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  const SecondRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding:const EdgeInsets.only(top:128),
      color:Colors.transparent,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Second Route'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Go back!'),
          ),
        ),
      ),
    );
  }
}

Solution

  • You can achieve this using bottom sheet.

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          // Remove the debug banner
          debugShowCheckedModeBanner: false,
          title: 'example',
          home: HomeScreen(),
        );
      }
    }
    
    class HomeScreen extends StatelessWidget {
      const HomeScreen({Key? key}) : super(key: key);
    
      void _show(BuildContext ctx) {
        showModalBottomSheet(
            elevation: 10,
            isScrollControlled: true,
            context: ctx,
            builder: (ctx) => Container(
                  //change height to change height of bottom sheet
                  height: MediaQuery.of(ctx).size.height * 0.75,
                  alignment: Alignment.center,
                  child: const Text('bottom sheet'),
                ));
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Example'),
          ),
          body: SafeArea(
            child: Center(
              child: ElevatedButton(
                child: const Text('Show The BottomSheet'),
                onPressed: () => _show(context),
              ),
            ),
          ),
        );
      }
    }
    

    enter image description here