Search code examples
flutterdartappbar

flutter:: I have a question about how to use the app top bar


This time I'm making an app top bar in flutter. I wrote code like this.

import 'package:flutter/material.dart';

class VideoAppBar extends StatelessWidget{
  const VideoAppBar({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context){
    return SafeArea(
      child: Scaffold(
        body: WillPopScope( // <- wraps only the Scaffold body.
          child: Center(
          ),
          onWillPop: () {

            return Future(() => false);
          },
        ),
        appBar: AppBar(

          backgroundColor: Colors.white,
          title: Text('Very verse',
            style: TextStyle(
              color: Colors.black,
              fontSize: 20,
            ),),
          centerTitle: true,
          leading: IconButton(
            onPressed: () {
              Navigator.pop(context);
            },
            color: Colors.black,
            iconSize: 25,
            icon: Icon(Icons.arrow_back),

          ),

          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.bookmark_outline),
              iconSize: 25,
              color: Colors.black,
              onPressed: () {
                print('GD');
              }
          ),
          ],
        ),
      ),
    );
  }
}

I'd like to put this code simply as a function on several other pages.

AppBar(title:VideoAppBar);

I figured it could be done like this.

But it doesn't go back.

How should I write the code?


Solution

  • Try below Code hope its helpful to you

    Create your regular class or your Widget like below:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(
        MyApp(),
      );
    }
    
    class MyApp extends StatelessWidget {
      bool shouldPop = true;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: WillPopScope(
            onWillPop: () async {
              return shouldPop;
            },
            child: Scaffold(
              appBar: appBar(context),
              body: Center(
                child: Text('Test'),
              ),
            ),
          ),
        );
      }
    }
    

    Create another dart file like appBar.dart and declare your Appbar Widget in dart file. call your widget any file on your need

    appBar(BuildContext context) {
      return AppBar(
        backgroundColor: Colors.white,
        title: Text(
          'Very verse',
          style: TextStyle(
            color: Colors.black,
            fontSize: 20,
          ),
        ),
        centerTitle: true,
        leading: IconButton(
          onPressed: () {
            Navigator.pop(context);
          },
          color: Colors.black,
          iconSize: 25,
          icon: Icon(Icons.arrow_back),
        ),
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.bookmark_outline),
              iconSize: 25,
              color: Colors.black,
              onPressed: () {
                print('GD');
              }),
        ],
      );
    }
    
    1. For WillPopScope class

    2. For Scaffold class

    3. For AppBar

    Your result screen -> Image