Advertisement
austin944

Flutter App

Feb 2nd, 2025 (edited)
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1.  
  2. import 'package:flutter/material.dart';
  3.  
  4. void main() {
  5. runApp(const GradientApp());
  6. }
  7.  
  8. class GradientApp extends StatelessWidget {
  9. const GradientApp({super.key});
  10.  
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. title: 'Gradient App',
  15. home: const GradientScreen(),
  16. );
  17. }
  18. }
  19.  
  20. class GradientScreen extends StatelessWidget {
  21. const GradientScreen({super.key});
  22.  
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. body: Container(
  27. decoration: const BoxDecoration(
  28. gradient: LinearGradient(
  29. begin: Alignment.topLeft,
  30. end: Alignment.bottomRight,
  31. colors: [
  32. Color(0xFFE66465), // Example color 1 - you can change these
  33. Color(0xFF9198E5), // Example color 2
  34. // Add more colors for a more complex gradient
  35. // Color(0xFF673AB7),
  36. ],
  37. ),
  38. ),
  39. child: Center(
  40. child: Column(
  41. mainAxisAlignment: MainAxisAlignment.center,
  42. children: <Widget>[
  43. // Your Logo/Icon
  44. Image.asset( // Replace with your actual asset path
  45. 'assets/your_logo.png', // Make sure to add the asset in pubspec.yaml
  46. height: 100, // Adjust size as needed
  47. ),
  48. const SizedBox(height: 20), // Spacing
  49.  
  50. ElevatedButton(
  51. onPressed: () {
  52. // Button 1 action
  53. print("Button 1 pressed");
  54. },
  55. style: ElevatedButton.styleFrom(
  56. backgroundColor: Colors.white, // Button color
  57. foregroundColor: Colors.black, // Text color
  58. ),
  59. child: const Text('Button 1'),
  60. ),
  61. const SizedBox(height: 10),
  62.  
  63. ElevatedButton(
  64. onPressed: () {
  65. // Button 2 action
  66. print("Button 2 pressed");
  67. },
  68. style: ElevatedButton.styleFrom(
  69. backgroundColor: Colors.white,
  70. foregroundColor: Colors.black,
  71. ),
  72. child: const Text('Button 2'),
  73. ),
  74.  
  75. const SizedBox(height: 10),
  76.  
  77. OutlinedButton( // Example of an OutlinedButton
  78. onPressed: () {
  79. print("Outlined Button pressed");
  80. },
  81. style: OutlinedButton.styleFrom(
  82. foregroundColor: Colors.white, // Text color
  83. side: const BorderSide(color: Colors.white), // Border color
  84. ),
  85. child: const Text("Outlined Button"),
  86. ),
  87.  
  88.  
  89. ],
  90. ),
  91. ),
  92. ),
  93. );
  94. }
  95. }
  96.  
  97.  
  98. **Key improvements and explanations:**
  99.  
  100. 1. **Gradient:** Uses `LinearGradient` within a `BoxDecoration` to create the gradient background. You can easily customize the `colors` array to achieve your desired gradient effect. I've provided two example colors, but you can add more for a more complex gradient. The `begin` and `end` properties control the direction of the gradient.
  101.  
  102. 2. **Logo/Icon:** Includes an `Image.asset` widget as a placeholder for your logo. **Crucially,** you need to:
  103. - Place your logo image (e.g., `your_logo.png`) in your project's `assets` folder (create this folder if it doesn't exist).
  104. - Declare the asset in your `pubspec.yaml` file under the `flutter` section, like this:
  105.  
  106. yaml:
  107.  
  108. flutter:
  109. assets:
  110. - assets/your_logo.png # Path to your logo
  111. - assets/ # To include all files in the folder
  112.  
  113.  
  114. 3. **Buttons:** Uses `ElevatedButton` for the buttons. I've added some basic styling (background and foreground colors). You can customize the button styles further. I've also included an `OutlinedButton` example.
  115.  
  116. 4. **Spacing:** Uses `SizedBox` widgets to add spacing between the logo and the buttons, making the layout cleaner.
  117.  
  118. 5. **Centering:** The `Center` widget ensures that the content is centered on the screen. The `Column` arranges the widgets vertically.
  119.  
  120. 6. **Comments:** Added comments to explain the code and guide you.
  121.  
  122. **How to run this app:**
  123.  
  124. 1. **Create a new Flutter project:** If you haven't already, create a new Flutter project using your IDE (Android Studio or VS Code) or the command line: `flutter create my_gradient_app`
  125. 2. **Replace code:** Replace the contents of `lib/main.dart` with the code provided above.
  126. 3. **Add your logo:** Place your logo image in the `assets` folder and update the `pubspec.yaml` file as explained above.
  127. 4. **Run the app:** Run the app using `flutter run`.
  128.  
  129. Remember to adjust the colors, button styles, logo path, and button actions to fit your specific design and functionality. This example provides a solid foundation for building your gradient app.
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement