英文:
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 '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'),
)
],
),
],
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论