【问题标题】:flutter layout trouble for add image above two button在两个按钮上方添加图像的颤动布局问题
【发布时间】:2021-07-06 04:57:37
【问题描述】:

我在 StackOver Flow 上发帖是因为我一直在开发 Flutter 应用程序。我是一个初学者,我正在学习 Flutter 和 Dart,但我被困在一些基本的东西上,这有点令人沮丧。

我想在移动应用程序的中间显示一张图片,下面有两个按钮。根据我单击的按钮,图像会发生变化。我正在尝试这样做以练习,但我被困在布局上。我可以显示两个按钮,但我无法将图像定位在上方

import 'package:flutter/material.dart';

const appBarColor_primary = Color(0xFFEE0245);

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(colorScheme: ColorScheme.light()),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Am I rich ?'),
          backgroundColor: appBarColor_primary,
        ),
        body: Center(
            child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.network(
                'https://dictionary.cambridge.org/fr/images/thumb/diamon_noun_002_10599.jpg?version=5.0.158'),
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(appBarColor_primary),
              ),
              onPressed: () {},
              child: Text('Yes I am Rich !'),
            ),
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(appBarColor_primary),
              ),
              onPressed: () {},
              child: Text(
                'No, I am poor !',
              ),
            ),
          ],
        )));
  }
}

你知道我该怎么做吗?

提前感谢您的宝贵帮助

【问题讨论】:

    标签: image flutter button layout


    【解决方案1】:

    您需要一个 Column 来垂直放置小部件:

    class MyHomePage extends StatelessWidget {
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: const Text('Am I rich ?'),
              backgroundColor: appBarColor_primary,
            ),
            body: Center(
                child: Column(
              children: [
                Image.network('https://dictionary.cambridge.org/fr/images/thumb/diamon_noun_002_10599.jpg?version=5.0.158'),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ElevatedButton(
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(appBarColor_primary),
                      ),
                      onPressed: () {},
                      child: Text('Yes I am Rich !'),
                    ),
                    ElevatedButton(
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(appBarColor_primary),
                      ),
                      onPressed: () {},
                      child: Text(
                        'No, I am poor !',
                      ),
                    ),
                  ],
                ),
              ],
            )));
      }
    }
    

    【讨论】:

    • 非常感谢 Esteve Aguilera,它成功了
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 2021-12-18
    • 2019-02-16
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2014-07-17
    相关资源
    最近更新 更多