【问题标题】:flutter:: I have a question about how to use the app top barflutter:: 我有一个关于如何使用应用顶部栏的问题
【发布时间】:2021-11-16 23:46:38
【问题描述】:

这次我正在制作一个应用程序顶部栏。我写了这样的代码。

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');
              }
          ),
          ],
        ),
      ),
    );
  }
}

我想把这段代码简单地作为一个函数放在其他几个页面上。

AppBar(title:VideoAppBar);

我认为可以这样做。

但它不会回去。

我应该如何编写代码?

【问题讨论】:

    标签: flutter dart appbar


    【解决方案1】:

    试试下面的代码希望对你有帮助

    创建您的常规课程或您的小部件,如下所示:

    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'),
              ),
            ),
          ),
        );
      }
    }
    

    创建另一个 dart 文件,如 appBar.dart 并在 dart 文件中声明您的 Appbar Widget。根据需要调用您的小部件任何文件

    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. 对于WillPopScope class

    2. 对于Scaffold class

    3. 对于AppBar

    您的结果屏幕 ->

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 2019-03-26
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多