To display “Hello, good morning” text when a button is clicked in Flutter, you can use StatefulWidget
to update the UI. Here’s a simple example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: GreetingScreen(),
);
}
}
class GreetingScreen extends StatefulWidget {
@override
_GreetingScreenState createState() => _GreetingScreenState();
}
class _GreetingScreenState extends State<GreetingScreen> {
String greetingText = "";
void showGreeting() {
setState(() {
greetingText = "Hello, good morning";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Greeting App')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
greetingText,
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: showGreeting,
child: Text('Click Me'),
),
],
),
),
);
}
}