Change int value to 0 via button

142

Let's try

import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Test(),
    );
  }
}

class Test extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TestState();
  }
}

class _TestState extends State<Test> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          child: InkWell(
            child: Icon(Icons.add),
            onTap: () {
              setState(() {
                counter++;
              });
            },
          ),
        ),
        appBar: AppBar(
          title: Text('My App'),
        ),
        body: Center(
          child: Column(
            children: [
              Text("$counter"),
              TextButton(
                onPressed: () {
                  setState(() {
                    counter = 0;
                  });
                },
                child: Text("Reset button"),
              ),
            ],
          ),
        ));
  }
}

output:

enter image description here

Share:
142
Zoe stands with Ukraine
Author by

Zoe stands with Ukraine

Pronouns: she/her Stand with Ukraine

Updated on December 07, 2022

Comments

  • Zoe stands with Ukraine
    Zoe stands with Ukraine over 1 year

    I am developing an counter app, and I have this floating action button which displays the amount of times it's been clicked. I want to create a reset clicks button, where the amount of the current clicks of the floating action button changes into 0 via the button.

    • NirmalCode
      NirmalCode over 2 years
      Include the code you tried, Errors that occurred, etc.
    • Jahidul Islam
      Jahidul Islam over 2 years
      please check the answer