avatar

松果工作室

欢迎光临

  • 首页
  • ESP
  • LVGL
  • freeRTOS
  • 快速笔记
  • 考察日志
  • 个人收藏
  • 我的服务
Home (Flutter) 带阴影的图标
文章

(Flutter) 带阴影的图标

Posted 2025-04-6 Updated 2025-04- 6
By YCP
14~18 min read

Code

基本的代码框架
这个代码框架是Flutter应用的标准结构,它遵循了一些最佳实践和设计模式。以下是为什么要这么写框架以及这样做的好处:

  1. 分离关注点:

    • MyApp类专注于应用的总体配置,如主题、路由等。
    • MyHomePage类专注于具体页面的实现。
    • _MyHomePageState类管理页面的状态。
  2. 易于维护和扩展:

    • 这种结构使得代码更加模块化,每个类都有明确的职责。
    • 当需要添加新功能或页面时,可以轻松扩展代码,而不需要修改现有代码。
  3. 遵循Flutter的最佳实践:

    • 使用StatelessWidget和StatefulWidget来构建应用。
    • StatefulWidget用于需要管理状态的组件,而StatelessWidget用于不需要管理状态的组件。
  4. 良好的错误处理:

    • 通过将状态管理分离到State类中,可以更容易地处理错误和异常情况。
  5. 易于测试:

    • 这种结构使得单元测试更加容易,因为每个组件都是独立的,可以单独测试。
  6. 代码复用:

    • 使用Widget类可以使代码更加模块化,从而提高代码的复用性。
  7. 结构清晰:

    • 这种结构清晰地分离了应用的不同部分,使得代码更易于理解和维护。
  8. 遵循SOLID原则:

    • 这种代码结构遵循了SOLID原则中的单一职责原则(Single Responsibility Principle),即每个类应该只负责一件事情。

总之,这种代码框架不仅使代码更加清晰和易于维护,而且还遵循了Flutter的最佳实践和软件工程的原则。这使得代码更加健壮、可靠和可扩展。

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> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[300],
      // appBar: AppBar(
      //   title: Text("My First App"),
      //   backgroundColor: Colors.blue,
      // ),
      body: Center(
        child: Container(
          width: 200,
          height: 200,
          //color: Colors.red,
          child: Center(
            child: Icon(
              Icons.apple,
              size: 80,
            ),
          ),
          decoration: BoxDecoration(
            color: Colors.grey[300],
            borderRadius: BorderRadius.circular(50),
            boxShadow: [
              BoxShadow(
                color: Colors.grey,
                offset: Offset(10, 10),
                blurRadius: 15,
                spreadRadius: 1.0,
              ),
              BoxShadow(
                color: Colors.white,
                offset: Offset(-10, -10),
                blurRadius: 15,
                spreadRadius: 1.0,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

效果

截屏2025-04-06 下午3.48.05.png

升级版 带切换明暗按钮

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> {
  bool darkMode = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: darkMode ? Colors.grey[850] : Colors.grey[300],
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 200,
              height: 200,
              //color: Colors.red,
              //child: Center(
              child: Icon(
                Icons.apple,
                size: 80,
                color: darkMode ? Colors.white:Colors.black,
              ),
              //),
              decoration: BoxDecoration(
                color: darkMode?Colors.grey[850]:Colors.grey[300],
                borderRadius: BorderRadius.circular(50),
                boxShadow: [
                  BoxShadow(
                    color: darkMode?Colors.grey.shade800:Colors.grey.shade900,
                    offset: Offset(10, 10),
                    blurRadius: 15,
                    spreadRadius: 1.0,
                  ),
                  BoxShadow(
                    color: darkMode?Colors.grey.shade900:Colors.grey.shade100,
                    offset: Offset(-10, -10),
                    blurRadius: 15,
                    spreadRadius: 1.0,
                  ),
                ],
              ),
            ),
            SizedBox(height: 20),
            Padding(
              padding: EdgeInsets.only(
                top: 50,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextButton(
                      onPressed: () {
                        setState(() {
                          darkMode = true;
                        });
                      },
                      child: Text(
                        'Dark',
                        style: TextStyle(color: Colors.white),
                      ),
                      style: TextButton.styleFrom(
                        backgroundColor: Colors.grey[900],
                      )),
                  SizedBox(width: 20),
                  TextButton(
                    onPressed: () {
                      setState(() {
                        darkMode = false;
                      });
                    },
                    child: Text(
                      'Light',
                      style: TextStyle(color: Colors.black),
                    ),
                    style: TextButton.styleFrom(
                      backgroundColor: Colors.grey[100],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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