简体   繁体   English

Flutter - 如何创建这种动画搜索栏?

[英]Flutter - How to create this kind of animated search bar?

What I need:我需要的:

在此处输入图像描述

Click on to the searchbar => Animated searchbar moves as shown单击搜索栏 => 动画搜索栏移动,如图所示

What I have tried:我试过的:

Currently, I am using Navigator push but the effect isn't sleek as how I would want it to be.目前,我正在使用 Navigator push,但效果并不像我想要的那样流畅。 Would love to find out if it is possible to achieve the intended effect shown above and how to do it.很想知道是否有可能达到上面显示的预期效果以及如何做到这一点。 Thanks!谢谢!

In the AppBar:在应用栏中:

InkWell(
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  alignment: Alignment.centerLeft,
                  width: MediaQuery.of(context).size.width * 0.80,
                  height: 35,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(
                      Radius.circular(10),
                    ),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.grey.withOpacity(0.5),
                          spreadRadius: 3,
                          blurRadius: 5)
                    ],
                  ),
                  child: Row(
                    children: [
                      Icon(Icons.search, size: 25, color: Colors.grey),
                      SizedBox(width: 5),
                      Text('Search for Food, Drinks, Items',
                          style: TextStyle(
                              color: Colors.black,
                              fontFamily: 'Poppins',
                              fontSize: 12))
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.of(context).push(
                    MaterialPageRoute(builder: (_) => SearchPage()),
                  );
                },
              )

SearchPage.dart SearchPage.dart

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          foregroundColor: Colors.black,
          backgroundColor: Colors.white,
          // The search area here
          title: Container(
            width: double.infinity,
            height: 40,
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(5)),
            child: Center(
              child: TextField(
                autofocus: true,
                decoration: InputDecoration(
                    prefixIcon: Icon(Icons.search, color: Colors.black),
                    suffixIcon: IconButton(
                      icon: Icon(Icons.clear, color: Colors.black),
                      onPressed: () {
                        /* Clear the search field */
                      },
                    ),
                    hintText: 'Search...',
                    border: InputBorder.none),
              ),
            ),
          )),
    );
  }
}

You can use Hero animations to do these animations between screens.您可以使用Hero 动画在屏幕之间制作这些动画。 What you need to do is to wrap the Search bar on both the screens with the Hero widget and add a unique id to it.您需要做的是使用Hero小部件将搜索栏包装在两个屏幕上,并为其添加一个唯一的 ID。

It is easy to implement, just read the official flutter documentation.实现起来很简单,阅读flutter官方文档即可。

Flutter has a class call SearchDelegate . Flutter 有一个类调用SearchDelegate It's a widget that allows you to implement the search functionnality in your app with this kind of effect.它是一个小部件,可让您在应用中实现具有这种效果的搜索功能。

You can find how to it here你可以在这里找到它的方法

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

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