禁用按钮的默认动画方法

huangapple go评论58阅读模式
英文:

How to disable default animation of a button

问题

我是新学习Flutter的,制作了一个具有两个轮廓按钮的应用,在特定条件下这两个按钮会启用或禁用。当按钮启用或禁用时会有短暂的延迟。问题如下:

如何去除这个延迟?我希望按钮能立即启用或禁用。有什么建议吗?

我正在模拟器上运行这个应用。

英文:

I'm new to learning flutter and have made an app that has two outlined buttons that get enabled or disabled on certain conditions. When the button is enabled or when it is disabled there is a short delay. Here's the problem:

禁用按钮的默认动画方法

How do I remove the delay? I want the button to be enabled or disabled instantly. Any ideas?

I am running the app on simulator.

答案1

得分: 1

您可以根据按钮是否禁用/启用来传递不同的键。

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.dark(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

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

  void _onRemovePressed() {
    setState(() {
      number--;
    });
  }

  void _onAddPressed() {
    setState(() {
      number++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Demo'),
      ),
      body: Center(
        child: Column(
          children: [
            Text(
              number.toString(),
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                OutlinedButton(
                  key: number <= 0 ? null : const ValueKey('RemoveButton'),
                  onPressed: number <= 0 ? null : _onRemovePressed,
                  child: const Text('Remove'),
                ),
                OutlinedButton(
                  onPressed: _onAddPressed,
                  child: const Text('Add'),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

<details>
<summary>英文:</summary>

You can pass different key to the outlined button in case button is disabled/enabled.

```dart
import &#39;package:flutter/material.dart&#39;;

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: &#39;Flutter Demo&#39;,
      theme: ThemeData.dark(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  int number = 0;

  void _onRemovePressed() {
    setState(() {
      number--;
    });
  }

  void _onAddPressed() {
    setState(() {
      number++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text(&#39;Demo&#39;),
      ),
      body: Center(
        child: Column(
          children: [
            Text(
              number.toString(),
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                OutlinedButton(
                  key: number &lt;= 0 ? null : const ValueKey(&#39;RemoveButton&#39;),
                  onPressed: number &lt;= 0 ? null : _onRemovePressed,
                  child: const Text(&#39;Remove&#39;),
                ),
                OutlinedButton(
                  onPressed: _onAddPressed,
                  child: const Text(&#39;Add&#39;),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年6月6日 15:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定