Bootstrap

Flutter用户交互组件

Creating interactive user interfaces is quite straightforward, thanks to widgets like GestureDetector and InkWell. These widgets allow you to capture a wide array of user interactions, from simple taps to complex gestures, making your app not just functional but also delightful to use.

有了 GestureDetector 和 InkWell 等小工具,创建交互式用户界面就变得简单易行了。通过这些小工具,您可以捕捉从简单的点击到复杂的手势等各种用户交互,使您的应用程序不仅功能强大,而且使用起来令人愉悦。

GestureDetector

Think of GestureDetector as the gateway to making your Flutter app responsive to user actions like taps, swipes, pinches, and more.

将 GestureDetector 视为让您的 Flutter 应用程序响应用户操作(如轻点、轻扫、轻按等)的入口。

But first, let’s set the stage. Imagine you’ve beautifully designed your e-commerce catalog page with those sleek product cards. It’s a visual delight, no doubt. However, at the moment, your users can’t do much except stare in awe. They can’t interact with the products - can’t tap on them to view details or add them to their cart. That’s where GestureDetector comes in.

但首先,让我们做个铺垫。想象一下,你已经设计好了精美的电子商务目录页面和时尚的产品卡片。毫无疑问,这是一种视觉享受。然而,此时此刻,您的用户除了目瞪口呆之外,什么也做不了。他们无法与产品互动–无法点击产品查看详情或将其添加到购物车。这就是 GestureDetector 的用武之地。

What is GestureDetector?

什么是 GestureDetector?

The GestureDetector widget is your key to adding interactivity to your Flutter app. It listens to gestures made by the user, such as taps, double taps, drags, and more, and triggers actions in response. It’s like adding a layer of functionality to your beautiful UI.

GestureDetector "部件是为 Flutter 应用程序添加交互性的关键。它可以监听用户做出的手势,如轻点、双击、拖动等,并触发相应的操作。它就像为您漂亮的用户界面添加了一层功能。

Let’s break down how you can use GestureDetector to enhance our Lagos store catalog page:

让我们来分析一下如何使用 GestureDetector 来增强我们的拉各斯商店目录页面:

Import the GestureDetector Widget:

导入 GestureDetector 组件:

First things first, you need to import material.dart. Make sure to add it to the top of your Dart file where your ecommerce catalog page is located.

首先,您需要导入 material.dart。确保将其添加到 Dart 文件的顶部,也就是电子商务目录页面的位置。

import ‘package:flutter/material.dart’;

Wrap Your Product Cards:

包装您的产品卡:

Now, find the part of your code where you’ve created those sleek product cards using the ProductCard widget. Wrap each ProductCard widget with a GestureDetector.

现在,找到代码中使用 ProductCard 部件创建时尚产品卡的部分。用 GestureDetector 对每个 ProductCard 部件进行包装。

GestureDetector(
  onTap: (){
    // 当用户点击的时候触发的操作
    print("用户点击了第$index个商品");
  },
  child: ProductCard(product: products[index]),
);

Setting the onTap property allows you to specify what should occur when a user taps on a product card. This could be navigating to a new screen with product details, incrementing a cart count, or taking any action of your choice.

设置 onTap 属性可让您指定当用户点击产品卡时应发生的操作。这可以是导航到一个包含产品详细信息的新屏幕、递增购物车计数或执行您选择的任何操作。

Rinse and Repeat:

冲洗并重复:

You can repeat this process for other gestures as well. For instance, you could implement a onDoubleTap property to handle double-tap actions.

您也可以为其他手势重复这一过程。例如,您可以实现一个 onDoubleTap 属性来处理双击操作。

And that’s it! You’ve now made your e-commerce catalog page interactive using the GestureDetector widget. Users can tap on products to view details, double-tap to add to favorites, and more, all thanks to the magic of Flutter.

就是这样!现在,您已经使用 GestureDetector widget 实现了电子商务目录页面的互动。用户可以轻点产品查看详细信息,双击产品添加到收藏夹等等,这一切都要归功于 Flutter 的神奇魔力。

Certainly! Let’s dive into the world of the InkWell widget in Flutter. It’s another powerful tool that you can use to handle user interactions and make your app more engaging.

当然!让我们深入了解 Flutter 中的 InkWell 小工具。这是另一个强大的工具,您可以用它来处理用户交互,让您的应用程序更吸引人。

完整代码:

//main.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'theme.dart';

void main() {
  runApp(const MyApp());
}

class ThemeManager {
  static void setThemeMode(ThemeMode mode) {
    Get.changeThemeMode(mode);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Lagos Store',
      theme: AppThemes.lightTheme,
      darkTheme: AppThemes.darkTheme,
      home: const CatalogPage(),
    );
  }
}

class CatalogPage extends StatelessWidget {
  const CatalogPage({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('在线商城'),
        actions: const [
          ThemeManagerWidget(),
        ],
      ),
      body: GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 0.7,
        ),
        itemCount: products.length,
        itemBuilder: (context, index) {
          // 使用手势检测组件包裹
          return GestureDetector(
            onTap: (){
              // 当用户点击的时候触发的操作
              print("用户点击了第$index个商品");
            },
            child: ProductCard(product: products[index]),
          );
        },
      ),
    );
  }
}

class Product {
  final String name;
  final String imageUrl;
  final double price; // Add the price field

  Product({required this.name, required this.imageUrl, required this.price});
}

final List<Product> products = [
  Product(name: 'White Garri', imageUrl: 'assets/1.jpg', price: 99.99),
  Product(name: 'Smoked Fish', imageUrl: 'assets/2.jpg', price: 149.99),
  Product(name: 'Bean Flour', imageUrl: 'assets/3.jpg', price: 199.99),
  // Add more products
];

class ProductCard extends StatelessWidget {
  final Product product;

  const ProductCard({super.key, required this.product});

  
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset(
            product.imageUrl,
            height: 100,
          ),
          const SizedBox(height: 8),
          Text(product.name),
          Text('\$${product.price.toStringAsFixed(2)}',
              style: const TextStyle(fontWeight: FontWeight.bold)),
        ],
      ),
    );
  }
}

class ThemeManagerWidget extends StatelessWidget {
  const ThemeManagerWidget({super.key});

  
  Widget build(BuildContext context) {
    return IconButton(
      icon: const Icon(Icons.brightness_4),
      onPressed: () {
        if (Get.isDarkMode) {
          ThemeManager.setThemeMode(ThemeMode.light);
        } else {
          ThemeManager.setThemeMode(ThemeMode.dark);
        }
      },
    );
  }
}

InkWell

The InkWell widget is a versatile component that’s mainly used to add touch interaction to your UI elements. Imagine you want to improve the look of the Lagos store product card custom widget and also want users to interact with it Right now, it’s static, and users can only look but not touch. Let’s see how InkWell can change that:

InkWell 组件是一个多功能组件,主要用于为用户界面元素添加触摸交互。想象一下,您想改进拉各斯商店产品卡自定义部件的外观,同时还想让用户与之互动。现在,它是静态的,用户只能看不能摸。让我们看看 InkWell 如何改变这种状况:

Import the InkWell Widget:

导入 InkWell 组件:

First, make sure you import material.dart at the beginning of your Dart file, where your e-commerce catalog page is defined.

首先,确保在 Dart 文件的开头导入 material.dart,这里定义了电子商务目录页面。

import ‘package:flutter/material.dart’;

Wrap Your Product Cards:

包装您的产品卡:

Locate the part of your code where you’ve designed your product cards using the ProductCard widget. Now, wrap each ProductCard widget with an InkWell.

找到代码中使用 ProductCard 部件设计产品卡片的部分。现在,用 InkWell 将每个 ProductCard 部件封装起来。

InkWell(
  onTap: (){
    // 当用户点击的时候触发的操作
    print("用户点击了第$index个商品");
  },
  child: ProductCard(product: products[index]),
);

Similar to GestureDetector, you set the onTap property to specify the action to take when a user taps on a product card. This could be navigating to a product details screen, adding it to the cart, or anything else you want.

与 “GestureDetector ”类似,您可以设置 “onTap ”属性,指定用户点击产品卡时要采取的操作。这可以是导航到产品详细信息屏幕、将其添加到购物车或其他任何您想要的操作。

Additional Features:

附加功能

The InkWell widget offers additional features such as onDoubleTap, onLongPress, and more. You can use these properties to handle other touch interactions, like doubletapping to add to favorites or long-pressing to open a context menu.

InkWell widget 提供额外的功能,如 onDoubleTap、onLongPress 等。您可以使用这些属性来处理其他触摸交互,如双击添加到收藏夹或长按打开上下文菜单。

Splash Effect:

飞溅效果:

One of the standout features of InkWell is its built-in splash effect. When a user taps the card, you get that ripple-like animation around the card, providing visual feedback that the tap was recognized. It’s both beautiful and functional!

InkWell 的突出特点之一是其内置的飞溅效果。当用户点击卡片时,卡片周围会出现类似涟漪的动画,为用户识别点击提供视觉反馈。它既美观又实用!

Rinse and Repeat:

冲洗并重复:

You can use the InkWell widget for various gestures and UI elements throughout your app. It’s a fantastic tool for creating interactive and responsive Flutter apps.

您可以在整个应用程序中使用 InkWell 部件来制作各种手势和 UI 元素。它是创建交互式响应 Flutter 应用程序的绝佳工具。

And there you have it! You’ve just made your Lagos store catalog page interactive using the InkWell widget. Users can now tap on products, double-tap to add to favorites, and experience that satisfying ripple animation, making your app more engaging and user-friendly.

就是这样!您刚刚使用 InkWell 小工具使您的拉各斯商店目录页面具有互动性。用户现在可以点击产品,双击添加到收藏夹,体验令人满意的涟漪动画,使您的应用程序更吸引人,更方便用户使用。

So, add InkWell to your Flutter app to enhance the user experience further. Happy coding!

因此,请将 InkWell 添加到您的 Flutter 应用程序中,以进一步增强用户体验。祝您编码愉快

;