简体   繁体   English

flutter:: 我有一个关于如何使用应用顶部栏的问题

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

This time I'm making an app top bar in flutter.这次我在 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.我想将此代码简单地作为 function 放在其他几个页面上。

AppBar(title:VideoAppBar); AppBar(title:VideoAppBar);

I figured it could be done like this.我想它可以这样做。

But it doesn't go back.但它没有 go 回来。

How should I write the code?我应该如何编写代码?

Try below Code hope its helpful to you试试下面的代码希望它对你有帮助

Create your regular class or your Widget like below:创建您的常规 class 或您的小部件,如下所示:

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.创建另一个 dart 文件,如 appBar.dart 并在 dart 文件中声明您的 Appbar Widget。 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. ForWillPopScope class适用于WillPopScope class

  2. For Scaffold class脚手架用 class

  3. For AppBar对于AppBar

Your result screen ->您的结果屏幕 -> 图片

**Using the top app bar The top app bar provides content and actions related to the current screen. **使用顶部应用栏顶部应用栏提供与当前屏幕相关的内容和操作。 It's used for branding, screen titles, navigation, and actions.它用于品牌、屏幕标题、导航和操作。

A regular top app bar can transform into a contextual action bar.常规的顶部应用栏可以转换为上下文操作栏。

Before you can use Material buttons, you need to import the Material Components package for Flutter:在使用 Material 按钮之前,您需要为 Flutter 导入 Material Components package:

package:flutter/material.dart You need to use MaterialApp. package:flutter/material.dart 你需要使用 MaterialApp。

For more information on getting started with the Material for Flutter, go to the Flutter Material library page.有关开始使用 Flutter、go 的材料的更多信息,请访问 Flutter 材料库页面。

Making the top app bar accessible Flutter's APIs support accessibility setting for large fonts, screen readers, and sufficient contrast.使顶部应用栏可访问 Flutter 的 API 支持大型 fonts、屏幕阅读器和足够对比度的可访问性设置。 For more information, go to Flutter's accessibility and internationalization pages.更多信息,go 到 Flutter 的可访问性和国际化页面。

For more guidance on writing labels, go to our page on how to write a good accessibility label.有关编写标签的更多指导,go 到我们的页面,了解如何编写良好的可访问性 label。

Types There are two types of top app bar: 1. regular top app bar 2. contextual action bar**类型 顶部应用栏有两种类型: 1. 常规顶部应用栏 2. 上下文操作栏**

AppBar(
leading: Icon(Icons.menu),
title: Text('Page title'),
actions: [
Icon(Icons.favorite),
  Padding(
    padding: EdgeInsets.symmetric(horizontal: 16),
    child: Icon(Icons.search),
    ),
   Icon(Icons.more_vert),
   ],
   backgroundColor: Colors.purple,
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM