Search code examples
androidflutterdartmobileflutter-layout

How to put button under an image? Flutter


Hello guys im new in flutter and i need help with my app, to put a button under an image in my app main menu,

here is my code so far i build based on https://docs.flutter.dev/development/ui/layout/tutorial

import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'Reminder/ui/home_reminder.dart';
import 'Reminder/ui/widgets/button.dart';

void main() {
  // debugPaintSizeEnabled = true;
  runApp(const HomePage());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Medicine Reminder App'),
        ),
        body: Stack(
          children: [
            Image.asset(
              'images/MenuImg.jpg',
              width: 600,
              height: 200,
              fit: BoxFit.cover,
            ),
          ],
        ),
      ),
    );
  }
}

what i wanna do is put a button to navigate to a different page.

Here is a little illustration where i want to put my button

Here is a little illustration where i want to put my button, thanks


Solution

  • You need to use the widget Column in the body argument of your Scaffold widget to have the possibility to add multiple widgets in a column view on your screen.

    You can find a fully working example at this zaap.run link.

    You can also find the code without any preview here:

    import 'package:flutter/material.dart';
    
    void main() {
      // debugPaintSizeEnabled = true;
      runApp(const HomePage());
    }
    
    class HomePage extends StatelessWidget {
      const HomePage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Medicine Reminder App'),
            ),
            body: Column(children: [
              Stack(
                children: [
                  Image.network(
                    'https://i.imgur.com/9ZOaH1m.jpeg',
                    width: 600,
                    height: 200,
                    fit: BoxFit.cover,
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  TextButton(
                    style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(Colors.black)),
                    onPressed: () {},
                    child: Text("Button1"),
                  ),
                  TextButton(
                    style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(Colors.black)),
                    onPressed: () {},
                    child: Text("Button2"),
                  ),
                  TextButton(
                    style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(Colors.black)),
                    onPressed: () {},
                    child: Text("Button3"),
                  ),
                ],
              )
            ]),
          ),
        );
      }
    }