简体   繁体   English

如何在 flutter 的 AppBar 中更改图标的位置?

[英]How to change positions of icons in AppBar in flutter?

I am trying to reposition my "compass" icon near the "search" icon from the App bar.我正在尝试将我的“指南针”图标重新定位在应用栏的“搜索”图标附近。 Everytime i try to make a change in the container with higher value 55 for example "padding: const EdgeInsets.symmetric(horizontal: 50)," i get error stack over flow.每次我尝试在具有更高值 55 的容器中进行更改时,例如“填充:const EdgeInsets.symmetric(horizontal:50)”,我都会收到错误堆栈溢出。 Also when i try to change the logo(png) the allingment, nothing happens.此外,当我尝试更改标识(png)时,没有任何反应。 Help please!请帮忙! Image图片

@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: <Color>[Colors.pink[400], Colors.blue[400]]
            ),
          ),
        ),
        title: Row(
          children: [
            Container(
              alignment: Alignment.center,
              height: 40,
              child: SizedBox(
                  child: Image.asset(
                    'assets/images/logox.png',
                  )
              ),
            ),
            Container(
                padding: const EdgeInsets.symmetric(horizontal: 50),
                child: SizedBox(
                    child: Icon(
                  Icons.explore_rounded,
                ))),
            // Container(child: Icon(Icons.explore_rounded)),
          ],
        ),
        actions: [
          GestureDetector(
            onTap: () {
              // authMethods.signOut();
              Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                    builder: (context) => SearchScreen(),
                  ));
            },
            child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: Icon(Icons.search)),
          )
        ],
      ),
      drawer: MainDrawer(),
      //body: Body(),

      bottomNavigationBar: GradientBottomNavigationBar(
        fixedColor: Colors.white,
        backgroundColorStart: Colors.pink[400],
        backgroundColorEnd: Colors.blue[400],
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(Icons.home), title: Text('Home')),
          BottomNavigationBarItem(
              icon: Icon(Icons.wallet_membership), title: Text('QUA')),
          BottomNavigationBarItem(
              icon: Icon(Icons.help), title: Text('Help')),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.pink,
        elevation: 50,
        hoverColor: Colors.red,
        autofocus: true,
        onPressed: () {
          print('hi');
        },
        child: Icon(
          Icons.comment,
        ),
        tooltip: 'Pick Image',
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat);
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
}

There is actions property in AppBar widget which is mainly used to put icons in AppBar . AppBar小部件中有actions属性,主要用于在AppBar中放置图标。 And in your case centerTitle is used to align your image to center在您的情况下, centerTitle用于将您的图像对齐到中心

Output Output

在此处输入图像描述

Full code完整代码

class MyHomePage extends StatefulWidget {
  final Function onItemPressed;

  MyHomePage({
    Key key,
    this.onItemPressed,
  }) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: <Color>[Colors.pink[400], Colors.blue[400]]),
          ),
        ),
        centerTitle: true,
        title: Container(
          alignment: Alignment.center,
          height: 40,
          child: SizedBox(
              child: Image.asset(
            'assets/logo.png',
          )),
        ),
        actions: [
          IconButton(
            onPressed: () {},
            icon: Icon(Icons.explore_rounded),
          ),
          IconButton(
            onPressed: () {},
            icon: Icon(Icons.search),
          )
        ],
      ),
    );
  }
}

Material appbar has an actions property, you can use that to put search and compass icon together. Material appbar 有一个actions属性,你可以用它把搜索和指南针图标放在一起。 In order to achieve the same remove为了达到同样的删除

Container(
   padding: const EdgeInsets.symmetric(horizontal: 50),
   child: SizedBox(
          child: Icon(
          Icons.explore_rounded,
 ))),

from its current position and add it into the actions property just above the search button.从其当前的 position 并将其添加到搜索按钮上方的actions属性中。

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

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