'RenderBox was not laid out' 使用 Expanded 后仍然出错

'RenderBox was not laid out' error even after using Expanded

我在一列中添加了两个卡片小部件在一行中
代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.fromLTRB(0.0, 10, 0.0, 0.0),
              child: Row(
                children: [
                  Expanded(
                    child: SizedBox(
                      height: 70,
                      child: Card(
                        color: Colors.orange[500],
                        child: ListTile(
                          leading: CircleAvatar(
                            backgroundImage:
                                AssetImage('assets/card_photo.png'),
                          ),
                          title: Text(
                            'Teacher of the month',
                            style: TextStyle(
                                fontSize: 10,
                                fontWeight: FontWeight.bold,
                                fontFamily: 'Poppins-Bold'),
                          ),
                          subtitle: Text(
                            'MAY 2020',
                            style: TextStyle(
                                fontSize: 8,
                                fontWeight: FontWeight.bold,
                                color: Colors.white,
                                fontFamily: 'Poppins-Bold'),
                          ),
                          onTap: () {},
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: SizedBox(
                      height: 70,
                      child: Card(
                        color: Colors.orange[500],
                        child: ListTile(
                          leading: CircleAvatar(
                            backgroundImage:
                                AssetImage('assets/card_photo.png'),
                          ),
                          title: Text(
                            'Teacher of the month',
                            style: TextStyle(
                                fontSize: 10,
                                fontWeight: FontWeight.bold,
                                fontFamily: 'Poppins-Bold'),
                          ),
                          subtitle: Text(
                            'CLASS NAME',
                            style: TextStyle(
                                fontSize: 8,
                                fontWeight: FontWeight.bold,
                                color: Colors.white,
                                fontFamily: 'Poppins-Bold'),
                          ),
                          onTap: () {},
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

这是输出:
Output Image
但是我希望这一行成为卡片的滚动小部件。但即使在使用扩展后,我也会收到 'RenderBox was not laid out' 错误。
这是它的代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.fromLTRB(0.0, 10, 0.0, 0.0),
              child: SingleChildScrollView(//added scrollview widget
                scrollDirection: Axis.horizontal,
                child: Row(
                  children: [
                    Expanded(
                      child: SizedBox(
                        height: 70,
                        child: Card(
                          color: Colors.orange[500],
                          child: ListTile(
                            leading: CircleAvatar(
                              backgroundImage:
                                  AssetImage('assets/card_photo.png'),
                            ),
                            title: Text(
                              'Teacher of the month',
                              style: TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.bold,
                                  fontFamily: 'Poppins-Bold'),
                            ),
                            subtitle: Text(
                              'MAY 2020',
                              style: TextStyle(
                                  fontSize: 8,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.white,
                                  fontFamily: 'Poppins-Bold'),
                            ),
                            onTap: () {},
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: SizedBox(
                        height: 70,
                        child: Card(
                          color: Colors.orange[500],
                          child: ListTile(
                            leading: CircleAvatar(
                              backgroundImage:
                                  AssetImage('assets/card_photo.png'),
                            ),
                            title: Text(
                              'Teacher of the month',
                              style: TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.bold,
                                  fontFamily: 'Poppins-Bold'),
                            ),
                            subtitle: Text(
                              'CLASS NAME',
                              style: TextStyle(
                                  fontSize: 8,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.white,
                                  fontFamily: 'Poppins-Bold'),
                            ),
                            onTap: () {},
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

编辑:我还想要图标下方的文字。如果有人也可以帮助我。示例图片 name icon

您应该使用 listView 小部件。

ListView(
  children: <Widget>[
    ItemOne(),
    ItemTwo(),
    ItemThree(),
  ],
),

并更改滚动物理使用:

scrollDirection: Axis.horizontal,

检查这是否有效

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class something extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var appBar = AppBar();
    return Container(
      height: MediaQuery.of(context).size.height / 3,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: 6,  
        itemExtent: MediaQuery.of(context).size.height / 3,
        itemBuilder: (context, index) {
          return _cards(context, appBar);
        },
      ),
    );
  }
}

Widget _cards(BuildContext context, AppBar appBar) {
  return Align(
    child: Container(
      height: 100,
      child: Card(
        semanticContainer: true,
        clipBehavior: Clip.antiAliasWithSaveLayer,
        elevation: 5,
        margin: EdgeInsets.all(10),
        color: Colors.orange[500],
        child: ListTile(
          leading: Column(
            children: [
              CircleAvatar(
                backgroundImage: NetworkImage(
                    'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
              ),
              Text("Name"),
            ],
          ),
          title: Text('Teacher of the month', style: _textStyle(10)),
          subtitle: Text('MAY 2020', style: _textStyle(8)),
          onTap: () {},
        ),
      ),
    ),
  );
}

_textStyle(double size) {
  return TextStyle(
      fontSize: size,
      fontWeight: FontWeight.bold,
      color: Colors.white,
      fontFamily: 'Poppins-Bold');
}

它给我这样的输出