【问题标题】:Building a UI interface for my flutter project with List view使用列表视图为我的颤振项目构建 UI 界面
【发布时间】:2021-09-24 12:02:21
【问题描述】:

我正在尝试为 Flutter 界面编写 UI 代码,并且在我添加“ListView”小部件之前一切顺利。我试图编译代码,它说构建成功完成。但是当我运行它时,虚拟 Android 屏幕加载了一点,然后变成了一片漆黑。我尝试了几次,但没有成功。请任何人都可以帮助我使用此代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Stateful Clicker Counter',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Clicker Counter Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 50.0),
                  child: FloatingActionButton(
                    onPressed: _incrementCounter,
                    tooltip: 'Increment',
                    child: Icon(
                      Icons.play_arrow,
                      size: 50.0,
                    ), //Icon
                  ), //Floatingactionbutton
                ), //Padding
                Padding(
                  padding: const EdgeInsets.only(top: 50.0),
                  child: FloatingActionButton(
                    onPressed: _incrementCounter,
                    tooltip: 'Increment',
                    child: Icon(
                      Icons.pause,
                      size: 50.0,
                    ), //Icon
                  ), //Floatingactionbutton
                ), //Padding
                Padding(
                  padding: const EdgeInsets.only(top: 50.0),
                  child: FloatingActionButton(
                    onPressed: _incrementCounter,
                    tooltip: 'Increment',
                    child: Icon(
                      Icons.mic,
                      size: 50.0,
                    ), //Icon
                  ), //FloatingactionButton
                ), //Padding
                ListView(
                  padding: const EdgeInsets.all(5.0),
                  children: <Widget>[
                    Container(
                      height: 30.0,
                      color: Colors.amber[600],
                      child: const Center(
                        child: Text(
                          'Entry A',
                          ), //Text
                        ), //Center
                    ), //Container
                    Container(
                      height: 30.0,
                      color: Colors.amber[500],
                      child: const Center(
                        child: Text(
                          'Entry B',
                          ), //Text
                        ), //Center
                    ), //Container
                    Container(
                      height: 30.0,
                      color: Colors.amber[100],
                      child: const Center(
                        child: Text(
                          'Entry C',
                        ), //Text
                      ), //Center
                    ), //Container
                  ], //End of Widget tree
                ), //ListView
              ], //End of submain Widget Tree
            ), //Row
          ], //End of Main Widget Tree
        ), //Column
      ), //Main Center
    ); //Scaffold
  }
}

P.S:我刚开始使用 Flutter,所以有些事情可能看起来没有必要

【问题讨论】:

  • 可以添加错误日志吗?

标签: flutter widget


【解决方案1】:

基于this post

尝试在Container 中添加ListView

更新:

现在这段代码应该可以工作了。问题是,您在Row 中添加了ListView 而不是Column

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Stateful Clicker Counter',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Clicker Counter Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key,this.title}) : super(key: key);

  final String? title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title!),
      ),
      body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 50.0),
                  child: FloatingActionButton(
                    onPressed: _incrementCounter,
                    tooltip: 'Increment',
                    child: Icon(
                      Icons.play_arrow,
                      size: 50.0,
                    ), //Icon
                  ), //Floatingactionbutton
                ), //Padding
                Padding(
                  padding: const EdgeInsets.only(top: 50.0),
                  child: FloatingActionButton(
                    onPressed: _incrementCounter,
                    tooltip: 'Increment',
                    child: Icon(
                      Icons.pause,
                      size: 50.0,
                    ), //Icon
                  ), //Floatingactionbutton
                ), //Padding
                Padding(
                  padding: const EdgeInsets.only(top: 50.0),
                  child: FloatingActionButton(
                    onPressed: _incrementCounter,
                    tooltip: 'Increment',
                    child: Icon(
                      Icons.mic,
                      size: 50.0,
                    ), //Icon
                  ), //FloatingactionButton
                ), //Padding
              ], //End of submain Widget Tree
            ), //Row
            Expanded(
              child: ListView(
                  children: <Widget>[
                    Container(
                      height: 10.0,
                      color: Colors.amber[600],
                      child: const Center(
                        child: Text(
                          'Entry A',
                          ), //Text
                        ), //Center
                    ), //Container
                    Container(
                      height: 10.0,
                      color: Colors.amber[500],
                      child: const Center(
                        child: Text(
                          'Entry B',
                          ), //Text
                        ), //Center
                    ), //Container
                    Container(
                      height: 10.0,
                      color: Colors.amber[100],
                      child: const Center(
                        child: Text(
                          'Entry C',
                        ), //Text
                      ), //Center
                    ), //Container
                  ], //End of Widget tree
                ), //ListView,
            )
          ], //End of Main Widget Tree
        ),
    ); //Scaffold
  }
}

【讨论】:

    猜你喜欢
    • 2018-10-26
    • 2020-01-18
    • 2020-01-16
    • 2021-06-02
    • 2020-05-10
    • 2020-08-14
    • 2022-01-18
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多