I have this certain issue so there is a URL in my flutter app where,
String URL = "www.abcd/page=$number";
int number = 1;
And a button with onTap where,
Inkwell(child:Container(child: Text("Next")),
onTap :()=> setState({
number )}
Sorry I typed those codes manually maybe there is some syntax error. So basically I want the user to tap on the next button the page number in the link increases by 1, but I want to iterate an 'if' function, when number> 1 then show a button where the page decrements by one and if number<1 then I don't want the container to show.
So is there any way to achieve it?
CodePudding user response:
Use a ternary operator
condition ? if True : if False
In your case use it on the on the button that decrements by 1.
number>1 ? shows Button : shows empty Sizedbox
number>1
? InkWell(
onTap: (() {
setState(() {
number--;
});}),
child: Container(
height: 30,
color: Colors.red[100],
child: Text('Previous'),
))
: SizedBox(),
CodePudding user response:
You can use if statement as shown below.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(child: MyHome()),
);
}
}
class MyHome extends StatefulWidget {
const MyHome({super.key});
@override
State<MyHome> createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
int number = 1;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
Text('URL www.abcd/page=$number'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
InkWell(
child: const Text("Next"),
onTap: () {
setState(() {
number ;
});
},
),
if (number > 1)
InkWell(
child: const Text("Previous"),
onTap: () {
setState(() {
number--;
});
},
),
],
),
]),
);
}
}