I'm a beginner in Flutter, I'm building a counter app and I wanted to add the functionalities of "On Clear" and "Decrease" not only increase the counter on click. I thought it'd be logical to add another two methods inside the class MyHomePage but it gives me the error of
Missing concrete implementation of 'State.build'.
I work another way around my code but what if I want to call a method inside that class that does that? Here's my code
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Hello Code Palace",
theme: ThemeData(primarySwatch: Colors.deepPurple),
home: MyHomePage(title: "Hello app!"));
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter;
});
/*void _decreaseCounter() {
setState(() {
--_counter;
});*/
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("The button has been pressed"),
Text(
" $_counter times",
style: new TextStyle(fontSize: 30.0),
)
],
),
),
floatingActionButton: Stack(children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 30.0, bottom: 24.0),
child: FloatingActionButton(
onPressed: ()=>setState(()=> --_counter),
tooltip: "Decrease",
child: Icon(Icons.remove),
),
),
Padding(
padding: const EdgeInsets.only(left: 265.0, bottom: 24.0),
child: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: "Increase",
child: Icon(Icons.add),
),
),
Padding(
padding: const EdgeInsets.only(left: 130.0, bottom: 24.0),
child: FloatingActionButton(
onPressed: ()=>setState(()=> _counter = 0),
tooltip: "Increase",
child: Icon(Icons.clear_all),
),
),
]),
);
}
}
If you see a way to arrange the buttons at the bottom in a more organized way I'd appreciate that as well.
CodePudding user response:
You should add new function outside of _incrementCounter()
.
Please check the below code :
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter;
});
}
void _decreaseCounter() {
setState(() {
--_counter;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
...
);
}
}