Flutter---默认程序(计数器)

实现效果

当点击右下角的加号按钮时,屏幕中间的数字会叠加,相当于计数器的功能。

整体架构

main.dart(入口层)-->MyApp(应用配置层)-->MyHomePage(页面层)-->_MyHomePageState(状态管理层)

页面绘制的逻辑

①main函数里面运行MyApp,

②MyApp创建Material设计环境,

③MaterialApp初始化首页,

④MyHomePage中的createState()创建_MyHomePageState实例

⑤_MyHomePageState中的build函数建造具体UI绘制到屏幕

构造函数

Dart 复制代码
const MyHomePage({super.key, required this.title});//接收title参数,required表示强制必须传递该参数

计数器的实现

①用户点击按钮

②调用_incrementCounter()

③执行_count++

④触发setState重建UI

修改一些代码,来改变页面UI

修改配色方案

Dart 复制代码
//把紫色变成蓝色
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),

效果图

修改成暗色模式,默认是亮色模式

Dart 复制代码
//修改为暗色模式
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue,brightness: Brightness.dark),

效果图

修改全局字体的字号

Dart 复制代码
在修改颜色的下面加
//colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        textTheme: TextTheme(  // 全局文字样式
          bodyMedium: TextStyle(fontSize: 20), // Flutter 3.x+
        ),

效果图

修改按钮的位置

Dart 复制代码
//centerTop:最上方的中间/centerFloat:最下方的中间
//endContained:右下角/endTop:右上角/startTop:左上角/startFloat:左下角
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, // 按钮的位置控制
//floatingActionButton: FloatingActionButton(

效果图

代码实例

main.dart

Dart 复制代码
import 'package:flutter/material.dart'; //导入Material设计组件库

void main() {
  runApp(const MyApp()); //应用入口
}

class MyApp extends StatelessWidget { //应用根目录 (StatelessWidget不可变UI)

  const MyApp({super.key});//默认构造函数

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo', //应用标题(显示在任务管理器)
      theme: ThemeData(   //全局主题配置
        //colorScheme:自动生成的配色方案,textTheme:预定义的文字样式
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),//完整配色方案(紫色)
        
      ),
      //首页,应用的第一个页面
      home: const MyHomePage(title: 'Flutter Demo Home Page'),//构造MyHomePage传入标题

    );
  }
}

class MyHomePage extends StatefulWidget { //主页(有状态),(StatefulWidget可变UI)

  //构造函数
  const MyHomePage({super.key, required this.title});//接收title参数,required表示强制必须传递该参数

  final String title; //存储通过构造函数传入的标题

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

class _MyHomePageState extends State<MyHomePage> { //主页状态管理

  int _counter = 0;//计数器状态

  void _incrementCounter() {
    setState(() { //调用setState()触发UI更新
      _counter++; //状态变更
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold( //页面骨架
      appBar: AppBar(

        backgroundColor: Theme.of(context).colorScheme.inversePrimary,

        title: Text(widget.title),//使用传入的标题
      ),
      body: Center(//居中布局

        child: Column( //垂直排列子组件
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

            const Text('You have pushed the button this many times:'),

            Text(
              '$_counter',//显示计数器值,数据绑定
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),

      //centerTop:最上方的中间/centerFloat:最下方的中间
//endContained:右下角/endTop:右上角/startTop:左上角/startFloat:左下角
//floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, // 按钮的位置控制
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter, //点击回调,事件绑定
        tooltip: 'Increment', //长按提示
        child: const Icon(Icons.add), //"+"图标
      ),
    );
  }
}
相关推荐
程序员老刘10 小时前
跨平台开发地图 | 2026年11月
flutter·ai编程·客户端
传奇开心果编程16 小时前
【Flutter踩坑排雷】第30课:异常未捕获坑|未捕获异常直接闪退,全局兜底故障处理
android·flutter·ios·移动开发·dart·ui框架·声明式ui
恋猫de小郭18 小时前
Flutter Native Interop 稳定了,Platform Channel 要退场了
android·前端·flutter
好的佩奇1 天前
Dart 之可迭代对象与生成器
android·flutter·dart
恋猫de小郭2 天前
Flutter Analyzer 系列坑推进,必看,AI 时代这玩意最吃你内存
android·前端·flutter
LinXunFeng2 天前
拿竞品 App 一扫,技术栈全暴露了
android·flutter·开源
传奇开心果编程3 天前
【Flutter踩坑排雷】第28课:平台目录缺失与修复坑|`flutter create` 的“重建”与“覆盖”边界
android·flutter·ios·移动开发·跨平台·dart·声明式ui
程序员老刘·3 天前
Flutter 3.50更新,开发者应该关注什么?
flutter·ai编程·跨平台开发
传奇开心果编程4 天前
【Flutter踩坑排雷】第25课:ScrollController 误用坑|跳转失效、监听泄漏与“控制器未附加”
android·flutter·ios·移动开发·跨平台·dart·声明式ui
程序员老刘4 天前
Flutter 3.50更新,开发者应该关注什么?
flutter·ai编程·客户端