【问题标题】:Nested CupertinoButtons: unnecessary highlighting of the parent button嵌套的 CupertinoButtons:父按钮的不必要突出显示
【发布时间】:2022-07-06 14:09:03
【问题描述】:

考虑以下代码:

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

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('My app'),
        ),
        body: Center(
          child: CupertinoButton(
            onPressed: () {},
            child: Container(
              height: 300,
              width: 300,
              color: Colors.red,
              child: Center(
                child: CupertinoButton(
                  onPressed: () {
                    debugPrint(
                        'Pressing this button shouldnt highlight the parent button');
                  },
                  child: Container(
                    width: 100,
                    height: 100,
                    color: Colors.green,
                  ),
                ),
              ),
            ),
          ),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}

它产生以下输出:

基本上,点击子按钮(绿色)也会突出显示父按钮(红色)。

如何防止按下子按钮时父按钮高亮?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您必须删除父库比蒂诺按钮

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            appBar: AppBar(
              title: const Text('My app'),
            ),
            body: Center(
              child: Container(
                height: 300,
                width: 300,
                color: Colors.red,
                child: Center(
                  child: CupertinoButton(
                    
                    onPressed: () {
                      debugPrint(
                          'Pressing this button shouldnt highlight the parent button');
                    },
                    child: Container(
                      width: 100,
                      height: 100,
                      color: Colors.green,
                    ),
                  ),
                ),
              ),
            ), // This trailing comma makes auto-formatting nicer for build methods.
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2017-11-03
      • 2020-08-22
      • 1970-01-01
      • 2015-09-27
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多