Thu. Nov 21st, 2024

Simply we create a stateful widget and in the stateful widget, we take the MyHomePage Name as you can see in the code. Now we return the scaffold just because we need the AppBar if you don’t need the app bar then you can return some things other like the material app or you can directly return the column. We created a function in the stateful widget which you can see in the code, first function is called when we tap on the Button. And also created the list of text which is called by the text widget while it changing.

After the AppBar we simply return the column in the body section and took the two widgets, first one is the text and the second one is the button. In the text widget, we took some font sized and called the list of text which I created upper side, and fetch the list text by adding dots and indexing. In flutter by default indexing is zero. In the button, we simply called our function which we created in the stateful widget state. while we tap on the button then the indexing changes and we fetch the text through the index so the text also changes with the help of a set state which we use in onbuttonclicked function which you can see in the code.

Main.Dart

import 'package:flutter/material.dart';

// ignore_for_file: prefer_const_constructors



void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AndroIndian',
      theme: ThemeData(
        // this is the primary color. which automatically
        // used by the appbar and elevated button
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  var currentindex = 0;

  void buttonClicked() {
    setState(() {
     
      currentindex = currentindex + 1;
    });
    
    print("button one");
  }

  @override
  Widget build(BuildContext context) {
    var questions = [
     
      "Raj is a Android developer",
      "You can also become the Flutter developer",
    ];

    return Scaffold(
      appBar: AppBar(
        
        title: Text("Test Message"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment
              .center,
          children: [
            Text(
              questions[
              currentindex],
              
              style: TextStyle(fontSize: 30),
            ),
            SizedBox(
              
              height:
              40,
            ),
            ElevatedButton(
              onPressed: buttonClicked,
              child: Text(
                  "Press Here"),
              
            ),
          ],
        ),
      ),
    );
  }
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *