flutter 如何在textField中使用tap on后缀?

izj3ouym  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(104)

我正在尝试写文本字段,它可以点击几乎所有的身体,除了后缀图标。后缀图标,我需要写特殊的行为。
我的密码是:

GestureDetector(
                    onTap: () => Navigator.of(context).push(
                      MaterialPageRoute(
                          builder: (context) => const SightSearchScreen()),
                    ),
                    child:
                     const SearchBar(
                      isEnabled: false,
                      isFocused: false,
                                       ),
                  ),

...并且搜索栏小部件是...(唯一的构建方法)

@override
   Widget build(BuildContext context) {
     return TextField(
       enableInteractiveSelection: widget.isEnabled? true:false, // will disable paste operation
       focusNode: widget.isEnabled? FocusNode():AlwaysDisabledFocusNode(),
       onSubmitted: widget.searchRequest,
       autofocus: widget.isFocused ? true : false,
       controller: controller,
       decoration: InputDecoration(
         floatingLabelBehavior: FloatingLabelBehavior.never,
         prefixIcon:
             const Icon(Icons.search_rounded, color: AppColors.inactiveBlack),
         suffixIcon: widget.isEnabled
             ? null
             : InkWell(
                 onTap: () => print("Inner GestDet Tapped"),
                 child: Icon(Icons.tune_rounded,
                     color: themeProvider.appTheme.filterButtonColor),
               ),
         

    ),

我绞尽脑汁...头部姿势检测器工作,如果我已经涵盖了吸收指针搜索栏,但内部墨水井不会工作。
谢谢你抽出时间。

hgc7kmma

hgc7kmma1#

要使用多脉冲手势检测器,请查看以下示例:

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

//Main function. The entry point for your Flutter app.
void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: DemoApp(),
      ),
    ),
  );
}

//   Simple demo app which consists of two containers. The goal is to allow multiple gestures into the arena.
//  Everything is handled manually with the use of `RawGestureDetector` and a custom `GestureRecognizer`(It extends `TapGestureRecognizer`).
//  The custom GestureRecognizer, `AllowMultipleGestureRecognizer` is added to the gesture list and creates a `GestureRecognizerFactoryWithHandlers` of type `AllowMultipleGestureRecognizer`.
//  It creates a gesture recognizer factory with the given callbacks, in this case, an `onTap`.
//  It listens for an instance of `onTap` then prints text to the console when it is called. Note that the `RawGestureDetector` code is the same for both
//  containers. The only difference being the text that is printed(Used as a way to identify the widget) 

class DemoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
      gestures: {
        AllowMultipleGestureRecognizer: GestureRecognizerFactoryWithHandlers<
            AllowMultipleGestureRecognizer>(
          () => AllowMultipleGestureRecognizer(),
          (AllowMultipleGestureRecognizer instance) {
            instance.onTap = () => print('Episode 4 is best! (parent container) ');
          },
        )
      },
      behavior: HitTestBehavior.opaque,
      //Parent Container
      child: Container(
        color: Colors.blueAccent,
        child: Center(
          //Wraps the second container in RawGestureDetector
          child: RawGestureDetector(
            gestures: {
              AllowMultipleGestureRecognizer:
                  GestureRecognizerFactoryWithHandlers<
                      AllowMultipleGestureRecognizer>(
                () => AllowMultipleGestureRecognizer(),  //constructor
                (AllowMultipleGestureRecognizer instance) {  //initializer
                  instance.onTap = () => print('Episode 8 is best! (nested container)');
                },
              )
            },
            //Creates the nested container within the first.
            child: Container(
               color: Colors.yellowAccent,
               width: 300.0,
               height: 400.0,
            ),
          ),
        ),
      ),
    );
  }
}

// Custom Gesture Recognizer.
// rejectGesture() is overridden. When a gesture is rejected, this is the function that is called. By default, it disposes of the
// Recognizer and runs clean up. However we modified it so that instead the Recognizer is disposed of, it is actually manually added.
// The result is instead you have one Recognizer winning the Arena, you have two. It is a win-win.
class AllowMultipleGestureRecognizer extends TapGestureRecognizer {
  @override
  void rejectGesture(int pointer) {
    acceptGesture(pointer);
  }
}

编码依据:https://gist.github.com/Nash0x7E2/08acca529096d93f3df0f60f9c034056

相关问题