最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android?Flutter實(shí)現(xiàn)搜索的三種方式詳解

 更新時(shí)間:2022年08月08日 08:49:20   作者:大前端之旅  
這篇文章主要為大家詳細(xì)介紹了Android?Flutter實(shí)現(xiàn)搜索的三種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下

示例 1 :使用搜索表單創(chuàng)建全屏模式

我們要構(gòu)建的小應(yīng)用程序有一個(gè)應(yīng)用程序欄,右側(cè)有一個(gè)搜索按鈕。按下此按鈕時(shí),將出現(xiàn)一個(gè)全屏模式對(duì)話框。它不會(huì)突然跳出來,而是帶有淡入淡出動(dòng)畫和幻燈片動(dòng)畫(從上到下)。在圓形搜索字段旁邊,有一個(gè)取消按鈕,可用于關(guān)閉模式。在搜索字段下方,我們會(huì)顯示一些搜索歷史記錄(您可以添加其他內(nèi)容,如建議、類別等)。

編碼

我們通過定義一個(gè)擴(kuò)展 ModalRoute 類的名為FullScreenSearchModal的類來創(chuàng)建完整模式。

main.dart中的完整源代碼及說明:

// 大前端之旅
// main.dart
import 'package:flutter/material.dart';
?
void main() => runApp(const MyApp());
?
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
?
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // remove the debug banner
      debugShowCheckedModeBanner: false,
      title: '大前端之旅',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const KindaCodeDemo(),
    );
  }
}
?
// this class defines the full-screen search modal
// by extending the ModalRoute class
class FullScreenSearchModal extends ModalRoute {
  @override
  Duration get transitionDuration => const Duration(milliseconds: 500);
?
  @override
  bool get opaque => false;
?
  @override
  bool get barrierDismissible => false;
?
  @override
  Color get barrierColor => Colors.black.withOpacity(0.6);
?
  @override
  String? get barrierLabel => null;
?
  @override
  bool get maintainState => true;
?
  @override
  Widget buildPage(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
  ) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // implement the search field
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: TextField(
                      autofocus: true,
                      decoration: InputDecoration(
                        contentPadding: const EdgeInsets.symmetric(
                            vertical: 0, horizontal: 20),
                        filled: true,
                        fillColor: Colors.grey.shade300,
                        suffixIcon: const Icon(Icons.close),
                        hintText: 'Search 大前端之旅',
                        border: OutlineInputBorder(
                            borderSide: BorderSide.none,
                            borderRadius: BorderRadius.circular(30)),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  // This button is used to close the search modal
                  TextButton(
                      onPressed: () => Navigator.of(context).pop(),
                      child: const Text('Cancel'))
                ],
              ),
?
              // display other things like search history, suggestions, search results, etc.
              const SizedBox(
                height: 20,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 5),
                child: Text('Recently Searched',
                    style:
                        TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              ),
              const ListTile(
                title: Text('Flutter tutorials'),
                leading: Icon(Icons.search),
                trailing: Icon(Icons.close),
              ),
              const ListTile(
                title: Text('How to fry a chicken'),
                leading: Icon(Icons.search),
                trailing: Icon(Icons.close),
              ),
              const ListTile(
                title: Text('大前端之旅'),
                leading: Icon(Icons.search),
                trailing: Icon(Icons.close),
              ),
              const ListTile(
                title: Text('Goodbye World'),
                leading: Icon(Icons.search),
                trailing: Icon(Icons.close),
              ),
              const ListTile(
                title: Text('Cute Puppies'),
                leading: Icon(Icons.search),
                trailing: Icon(Icons.close),
              )
            ],
          ),
        ),
      ),
    );
  }
?
  // animations for the search modal
  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    // add fade animation
    return FadeTransition(
      opacity: animation,
      // add slide animation
      child: SlideTransition(
        position: Tween<Offset>(
          begin: const Offset(0, -1),
          end: Offset.zero,
        ).animate(animation),
        child: child,
      ),
    );
  }
}
?
// This is the main screen of the application
class KindaCodeDemo extends StatelessWidget {
  const KindaCodeDemo({Key? key}) : super(key: key);
?
  void _showModal(BuildContext context) {
    Navigator.of(context).push(FullScreenSearchModal());
  }
?
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('大前端之旅'), actions: [
        // this button is used to open the search modal
        IconButton(
          icon: const Icon(Icons.search),
          onPressed: () => _showModal(context),
        )
      ]),
      body: Container(),
    );
  }
}

示例 2:AppBar 內(nèi)的搜索字段(最常見于娛樂應(yīng)用程序)

通常,許多娛樂應(yīng)用程序(包括 Facebook、Youtube、Spotify 等大型應(yīng)用程序)默認(rèn)不顯示搜索字段,而是顯示搜索圖標(biāo)按鈕。按下此按鈕時(shí),將顯示搜索字段。

我們要制作的演示應(yīng)用程序包含 2 個(gè)屏幕(頁面):HomePageSearchPage。用戶可以通過點(diǎn)擊搜索圖標(biāo)按鈕從主頁移動(dòng)到搜索頁面。搜索字段將通過使用SearchPage 的 AppBar的title參數(shù)來實(shí)現(xiàn)。

讓我們看看它是如何工作的:

編碼

./lib/main.dart中的完整源代碼及說明:

// main.dart
import 'package:flutter/material.dart';
?
void main() {
  runApp(const MyApp());
}
?
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
?
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: '大前端之旅',
        theme: ThemeData(
          primarySwatch: Colors.indigo,
        ),
        home: const HomePage());
  }
}
?
// Home Page
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);
?
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('大前端之旅'),
        actions: [
          // Navigate to the Search Screen
          IconButton(
              onPressed: () => Navigator.of(context)
                  .push(MaterialPageRoute(builder: (_) => const SearchPage())),
              icon: const Icon(Icons.search))
        ],
      ),
    );
  }
}
?
// Search Page
class SearchPage extends StatelessWidget {
  const SearchPage({Key? key}) : super(key: key);
?
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          // The search area here
          title: Container(
        width: double.infinity,
        height: 40,
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(5)),
        child: Center(
          child: TextField(
            decoration: InputDecoration(
                prefixIcon: const Icon(Icons.search),
                suffixIcon: IconButton(
                  icon: const Icon(Icons.clear),
                  onPressed: () {
                    /* Clear the search field */
                  },
                ),
                hintText: 'Search...',
                border: InputBorder.none),
          ),
        ),
      )),
    );
  }
}

示例 3:搜索字段和 SliverAppBar

廣告搜索是許多電子商務(wù)應(yīng)用程序最重要的功能之一,因此它們通常以最容易識(shí)別的方式顯示搜索字段,并且從一開始就占用大量空間(亞馬遜、Shopee 等)。

編碼

// main.dart
import 'package:flutter/material.dart';
?
void main() {
  runApp(const MyApp());
}
?
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
?
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: '大前端之旅',
        theme: ThemeData(
          primarySwatch: Colors.deepPurple,
        ),
        home: const HomePage());
  }
}
?
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
?
  @override
  State<HomePage> createState() => _HomePageState();
}
?
class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            floating: true,
            pinned: true,
            snap: false,
            centerTitle: false,
            title: const Text('大前端之旅'),
            actions: [
              IconButton(
                icon: const Icon(Icons.shopping_cart),
                onPressed: () {},
              ),
            ],
            bottom: AppBar(
              title: Container(
                width: double.infinity,
                height: 40,
                color: Colors.white,
                child: const Center(
                  child: TextField(
                    decoration: InputDecoration(
                        hintText: 'Search for something',
                        prefixIcon: Icon(Icons.search),
                        suffixIcon: Icon(Icons.camera_alt)),
                  ),
                ),
              ),
            ),
          ),
          // Other Sliver Widgets
          SliverList(
            delegate: SliverChildListDelegate([
              const SizedBox(
                height: 400,
                child: Center(
                  child: Text(
                    'This is an awesome shopping platform',
                  ),
                ),
              ),
              Container(
                height: 1000,
                color: Colors.pink,
              ),
            ]),
          ),
        ],
      ),
    );
  }
}

結(jié)論

您已經(jīng)研究了在 Flutter 中實(shí)現(xiàn)全屏搜索框的端到端示例。這種搜索方式如今非常流行,您可以在許多大型應(yīng)用程序和移動(dòng)網(wǎng)站中注意到它。

到此這篇關(guān)于Android Flutter實(shí)現(xiàn)搜索的三種方式詳解的文章就介紹到這了,更多相關(guān)Android Flutter搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android Studio項(xiàng)目適配AndroidX(Android 9.0)的方法步驟

    Android Studio項(xiàng)目適配AndroidX(Android 9.0)的方法步驟

    這篇文章主要介紹了Android Studio項(xiàng)目適配AndroidX(Android 9.0)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Android編程之通知欄的用法小結(jié)

    Android編程之通知欄的用法小結(jié)

    這篇文章主要介紹了Android編程之通知欄的用法,結(jié)合實(shí)例形式總結(jié)分析了Android通知欄的相關(guān)操作技巧,包括發(fā)送、刪除通知、自定義布局等操作實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-01-01
  • Android消息機(jī)制原理深入分析

    Android消息機(jī)制原理深入分析

    這篇文章主要介紹了Android消息機(jī)制原理,Android的消息機(jī)制主要是指Handler的運(yùn)行機(jī)制以及Handler所附帶的MessageQueue和Looper的工作過程
    2022-12-12
  • 幾個(gè)Android編程時(shí)需要注意的 web 問題

    幾個(gè)Android編程時(shí)需要注意的 web 問題

    這篇文章主要介紹了幾個(gè)Android編程時(shí)需要注意的 web 問題,需要的朋友可以參考下
    2014-12-12
  • ListView 分頁加載更新實(shí)例分享

    ListView 分頁加載更新實(shí)例分享

    ListView是android中最常用的控件之一,本文將詳細(xì)介紹此功能的實(shí)現(xiàn)
    2012-11-11
  • flutter實(shí)現(xiàn)點(diǎn)擊事件

    flutter實(shí)現(xiàn)點(diǎn)擊事件

    這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)點(diǎn)擊事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Android中activity從創(chuàng)建到顯示的基本介紹

    Android中activity從創(chuàng)建到顯示的基本介紹

    這篇文章主要給大家介紹了關(guān)于Android中activity從創(chuàng)建到顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android初學(xué)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。
    2017-11-11
  • Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫

    Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 詳解Android開發(fā)中Activity的四種launchMode

    詳解Android開發(fā)中Activity的四種launchMode

    這篇文章主要介紹了Android開發(fā)中Activity的四種launchMode,launchMode主要用于控制多個(gè)Activity間的跳轉(zhuǎn),需要的朋友可以參考下
    2016-03-03
  • ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法

    ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法

    下面小編就為大家分享一篇ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01

最新評(píng)論

塔河县| 腾冲县| 长阳| 甘肃省| 松阳县| 永胜县| 肥乡县| 绥芬河市| 荔浦县| 教育| 永康市| 桐庐县| 沛县| 含山县| 扎兰屯市| 海南省| 琼结县| 宁夏| 仁化县| 五峰| 英德市| 西吉县| 墨竹工卡县| 海晏县| 祥云县| 沙田区| 民权县| 安溪县| 长顺县| 呼伦贝尔市| 太原市| 原阳县| 博罗县| 北票市| 新丰县| 科技| 平和县| 揭东县| 临海市| 洪湖市| 玉溪市|