【问题标题】:FLUTTER: Divider() in Column inside a Row does not appear颤振:行内的列中的分隔符()不出现
【发布时间】:2022-07-21 23:46:49
【问题描述】:

我需要在行内的列中放置一个分隔符,但没有出现分隔符。我看到一些关于不应使用 Row 的问题,但就我而言,我需要 Row。这是最简单的代码,我需要显示分隔符

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Column(
              children: const [
                Text('up'),
                Divider(
                  color: Colors.black,
                  thickness: 2,
                ),
                Text('bottom'),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    SizedBox() 扭曲你的Divider()

     SizedBox(
                      width: MediaQuery.of(context).size.width,
                      child: Divider(
                        color: Colors.black,
                        thickness: 2,
                      ),
                    ),
    

    完整的可运行sn-p:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp();
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({required this.title});
      final String title;
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: [
                    Text('up'),
                    SizedBox(
                      width: MediaQuery.of(context).size.width,
                      child: Divider(
                        color: Colors.black,
                        thickness: 2,
                      ),
                    ),
                    Text('bottom'),
                  ],
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-11
      • 2020-11-15
      • 2018-04-24
      • 2020-01-05
      • 2021-08-11
      • 2023-01-07
      • 2020-09-30
      • 2022-08-02
      相关资源
      最近更新 更多