Created
October 6, 2021 12:20
-
-
Save iampato/c8aa9e1cc5f4707b1ba225e393a9347e to your computer and use it in GitHub Desktop.
Mula example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| class Increment extends StatefulWidget { | |
| const Increment({Key? key}) : super(key: key); | |
| @override | |
| State<Increment> createState() => _IncrementState(); | |
| } | |
| class _IncrementState extends State<Increment> { | |
| late TextEditingController _controller; | |
| @override | |
| void initState() { | |
| _controller = TextEditingController(text: '0') | |
| ..addListener(() { | |
| setState(() {}); | |
| }); | |
| super.initState(); | |
| } | |
| @override | |
| void dispose() { | |
| _controller.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| TextFormField( | |
| controller: _controller, | |
| keyboardType: TextInputType.number, | |
| ), | |
| Row( | |
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| children: [ | |
| TextButton( | |
| onPressed: () { | |
| final value = | |
| int.parse(_controller.text); //convert string to int | |
| _controller.text = (value + 1) | |
| .toString(); // increment then assign the controller value | |
| }, | |
| child: const Text("Add"), | |
| ), | |
| TextButton( | |
| onPressed: () { | |
| final value = | |
| int.parse(_controller.text); //convert string to int | |
| _controller.text = (value - 1) | |
| .toString(); // decrement then assign the controller value | |
| }, | |
| child: const Text("Minus"), | |
| ), | |
| ], | |
| ) | |
| ], | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment