avatar

松果工作室

欢迎光临

  • 首页
  • ESP
  • LVGL
  • freeRTOS
  • 快速笔记
  • 考察日志
  • 个人收藏
  • 我的服务
Home (Flutter)一种简单布局
文章

(Flutter)一种简单布局

Posted 2025-04-9 Updated 2025-04- 9
By YCP
5~7 min read

CODE

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(debugShowCheckedModeBanner: false, home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List _posts = ['post1', 'post2', 'post3'];
  final List _stories = [
    'story1',
    'story2',
    'story3',
    'story4',
    'story5',
    'story6'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Container(
          height: 150,
          child: ListView.builder(
            itemCount: _stories.length,
            scrollDirection: Axis.horizontal,
            itemBuilder: (context, index) {
              return MyCircle(child: _stories[index]);
            },
          ),
        ),
        Expanded(
          flex: 5,
          child: ListView.builder(
            itemCount: _posts.length,
            itemBuilder: (context, index) {
              return MySquare(
                child: _posts[index],
              );
            },
          ),
        )
      ],
    ));
  }
}

class MySquare extends StatelessWidget {
  final String child;

  MySquare({required this.child});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(8),
      child: Container(
        height: 200,
        color: Colors.blue,
        child: Center(
          child: Text(
            child,
            style: TextStyle(
              fontSize: 40,
            ),
          ),
        ),
      ),
    );
  }
}

class MyCircle extends StatelessWidget {
  final String child;

  MyCircle({required this.child});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(8),
      child: Container(
        height: 100,
        width: 100,
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: Colors.pink,
        ),
        child: Center(
          child: Text(
            this.child,
            style: TextStyle(fontSize: 24),
          ),
        ),
      ),
    );
  }
}

预览

截屏2025-04-09 下午8.23.25.png

Flutter
License:  CC BY 4.0
Share

Further Reading

OLDER

(Flutter) 侧边导航栏的实现

NEWER

(Flutter) 带阴影的图标

Recently Updated

  • (ESP-IDF)LVGL 模拟器
  • (ESP-IDF)LVGL 自定义对象加入编码器组
  • (ESP-IDF)vscode配置文件
  • (Elec)来复再生式晶体管单管收音机
  • (ESP-IDF)ESPNOW

Trending Tags

LVGL WCH Linux Elec ThatProject freeRTOS STM ESP Flutter Others

Contents

©2026 松果工作室. Some rights reserved.

Using the Halo theme Chirpy