If you want to disable button after first click in flutter? Instead of using RaisedButton directly, you can turn it into a StatefulWidget. Then use the ChangeNotifier to change it state from enable to disable and control button press function.It will also help you to reuse it in different places. Here is an example how can you do that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 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> { final ValueNotifier<MyButtonState> _myButtonStateChangeNotifier = ValueNotifier(MyButtonState.enable); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: MyButton( buttonStateChangeNotifier: _myButtonStateChangeNotifier, onPressed: _onButtonPressed, text: "Click Me", ), ), ); } _onButtonPressed() { print("Button Pressed"); _myButtonStateChangeNotifier.value = MyButtonState.disable; } } enum MyButtonState {enable, disable} class MyButton extends StatefulWidget { final VoidCallback onPressed; final String text; final TextStyle textStyle; final ValueNotifier<MyButtonState> buttonStateChangeNotifier; MyButton({ @required this.onPressed, this.text = "", this.textStyle, this.buttonStateChangeNotifier, }); @override _MyButtonState createState() => _MyButtonState(); } class _MyButtonState extends State<MyButton> { MyButtonState _myButtonState = MyButtonState.enable; @override void initState() { super.initState(); if (widget.buttonStateChangeNotifier != null) { widget.buttonStateChangeNotifier.addListener(_handleButtonStateChange); _myButtonState = widget.buttonStateChangeNotifier.value; } } @override Widget build(BuildContext context) { return RaisedButton( shape: RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(4)), ), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text(widget.text) ], ), onPressed: _myButtonState == MyButtonState.enable ? _handleOnPress : null, ); } _handleButtonStateChange() { setState(() { _myButtonState = widget.buttonStateChangeNotifier.value; }); } _handleOnPress() { if (_myButtonState == MyButtonState.enable) { widget.onPressed(); } } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.