Flutter:如何使整行可点击

use*_*135 10 flutter

如何在颤动中使整行可点击?。我将以下代码包装在“GestureDetector”中。该行中的单个项目可点击,但小部件周围的空白区域不可点击。

if ((auth.isLoggedIn)) ...[
   GestureDetector(
     onTap: () {
        auth.signOut();
     },
     child: buildItem(
        Image.asset("assets/images/logoff.png",),
        "logout"
     ),
   ),
],
Run Code Online (Sandbox Code Playgroud)

这是“buildItem”方法

Widget buildItem(Widget icon, String title) {
    return Padding(
      padding: const EdgeInsets.only(left: 20, right: 15),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
              child: Row(
            children: <Widget>[
              icon,
              SizedBox(width: 20.0),
              Text(
                title,
                style: TextStyle(
                  fontSize: 16.0,
                ),
              ),
            ],
          )),
          Icon(
            Icons.arrow_forward_ios,
            size: 20.0,
          ),
        ],
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

这是输出。行中的每个项目(图标、文本和 >)都会响应点击手势。但是,空白区域(例如文本和“>”之间)不响应点击

如何让整行响应点击。

在此处输入图片说明

小智 21

  child: GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
      ///
      },
Run Code Online (Sandbox Code Playgroud)


chu*_*han 19

用 InkWell 替换 GestureDetector 效果很好

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            InkWell(
              onTap: () {
                print("click");
              },
              child: buildItem(
                  Image.network("https://picsum.photos/250?image=9",),
                  "logout"
              ),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Widget buildItem(Widget icon, String title) {
  return Padding(
      padding: const EdgeInsets.only(left: 20, right: 15),
      child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Container(
                child: Row(
                  children: <Widget>[
                    icon,
                    SizedBox(width: 20.0),
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 16.0,
                      ),
                    ),
                  ],
                )),
            Icon(
              Icons.arrow_forward_ios,
              size: 20.0,
            ),
          ],
      ),
  );
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明