flutter 如何禁用按钮的默认动画

ni65a41a  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(143)

我刚开始学习Flutter,并制作了一个应用程序,其中有两个概述按钮,可以在某些条件下启用或禁用。当按钮启用或禁用时,会有短暂的延迟。问题来了:

如何消除延迟?我想立即启用或禁用按钮。有什么想法吗
我在模拟器上运行应用程序。

ejk8hzay

ejk8hzay1#

如果按钮被禁用/启用,您可以将不同的密钥传递给概述的按钮。

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'),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

相关问题