Flutter AppBar, 如何自定义

huangapple go评论79阅读模式
英文:

Flutter AppBar, how to customize

问题

我是新手使用Flutter,我很难理解AppBar的工作原理。

我想要得到类似这样的结果
Flutter AppBar, 如何自定义

我的主要问题是如何在leading部分添加一个类似图片中的橙色框,这就是我想要添加的内容:Flutter AppBar, 如何自定义

我对leading的问题在于,它具有固定的大小,如果内部内容增大,就会溢出,所以我考虑创建一个使用行和列的自定义应用栏,有关如何继续的任何建议吗?

提前致谢。

英文:

I'm new using Flutter and and I'm having trouble understanding how the AppBar works.

I would like to get a result similar to this
Flutter AppBar, 如何自定义

my main problem is how to add a box, similar to the orange one in the picture, inside the leading, that's what i want to add exactly: Flutter AppBar, 如何自定义.

the problem I have with the leading is that, having a fixed size, if what's inside increases in size it overflows, so I thought of creating a custom app bar using rows and columns, any suggestions on how to proceed ?

Thanks in advance.

答案1

得分: 2

I'm try To Solve Your Problem

**Here is Code:**

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            title: TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: Colors.purple,
                  shape: RoundedRectangleBorder(
                    borderRadius:  BorderRadius.circular(999),
                  ),
                ),
                onPressed: () {  },
                child: Text('1644  $', style: TextStyle(color: Colors.white))
            ),
            elevation: 0,
            actions: [
              Row(
                children: [

                  Text("Aujourd'Hui", style: TextStyle(color: Colors.black,fontSize: 19,fontWeight: FontWeight.bold),),
                  SizedBox(
                    width: 60,
                  ),
                  const SizedBox(width: 10),
                  Icon(Icons.contact_mail, color: Colors.black),
                  const SizedBox(width: 10),
                  Icon(Icons.settings, color: Colors.black),
                  SizedBox(
                    width: 23,
                  ),
                ],
              ),
            ],
          );
        }
    }

Flutter AppBar, 如何自定义


<details>
<summary>英文:</summary>

I&#39;m try To Solve Your Problem

**Here is Code:**

    import &#39;package:flutter/cupertino.dart&#39;;
    import &#39;package:flutter/material.dart&#39;;
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            title: TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: Colors.purple,
                  shape: RoundedRectangleBorder(
                    borderRadius:  BorderRadius.circular(999),
                  ),
                ),
                onPressed: () {  },
                child: Text(&#39;1644  $&#39;, style: TextStyle(color: Colors.white))
            ),
            elevation: 0,
            actions: [
              Row(
                children: [
    
                  Text(&quot;Aujourd&#39;Hui&quot;, style: TextStyle(color: Colors.black,fontSize: 19,fontWeight: FontWeight.bold),),
                  SizedBox(
                    width: 60,
                  ),
                  const SizedBox(width: 10),
                  Icon(Icons.contact_mail, color: Colors.black),
                  const SizedBox(width: 10),
                  Icon(Icons.settings, color: Colors.black),
                  SizedBox(
                    width: 23,
                  ),
                ],
              ),
            ],
          ),
            // body: SafeArea(
            //   child: Container(
            //     padding: const EdgeInsets.symmetric(horizontal: 10),
            //     child: Row(
            //       children: [
            //         Expanded(child: Text(&quot;Username&quot;, style: TextStyle(color: Colors.black),)),
            //         const SizedBox(width: 10),
            //         Icon(Icons.message, color: Colors.black),
            //         const SizedBox(width: 10),
            //         TextButton(
            //             style: TextButton.styleFrom(
            //               backgroundColor: Colors.purple,
            //               shape: RoundedRectangleBorder(
            //                 borderRadius:  BorderRadius.circular(999),
            //               ),
            //             ),
            //             onPressed: () {  },
            //             child: Text(&#39;Edit&#39;, style: TextStyle(color: Colors.white))
            //         ),
            //       ],
            //     ),
            //     height: kToolbarHeight,
            //   ),
            // ));
        );
      }
    }
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/24RsO.jpg

</details>



# 答案2
**得分**: 0

在这里只是一小段代码。查看它

在leading属性中添加小部件或图标以在一开始显示。

注意:我已经添加了图标,您可以添加任何小部件来替代它。

```dart
AppBar(
  leading: IconButton(
    icon: Icon(Icons.menu),
    onPressed: () {},
  ),
  title: Text("自定义应用"),
  actions: [
    IconButton(
      icon: Icon(Icons.settings),
      onPressed: () {},
    ),
    IconButton(
      icon: Icon(Icons.search),
      onPressed: () {},
    ),
  ],
)

这就是你可以做的。Flutter加油!

英文:

Here is just a small piece of code. Look into it

Add widget or icon in leading props to show in the very beginning.

note: I had added the icon, you can add any widget instead of that.

AppBar(
  leading: IconButton(
    icon: Icon(Icons.menu),
    onPressed: () {},
  ),
title: Text(&quot;Customize App&quot;),
),
actions: [
    IconButton(
      icon: Icon(Icons.settings),
      onPressed: () {},
    ),
    IconButton(
      icon: Icon(Icons.search),
      onPressed: () {},
    ),
  ],

This how you can do. Cheers flutter!

huangapple
  • 本文由 发表于 2023年2月27日 18:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定