Flutter 之StatefulWidget有状态组件,text 使用系统默认Theme.of(context).textTheme.titleSmall样式,自定义组件之间传参使用、

文章详细介绍了如何在Flutter中使用默认文本样式,有状态组件的基础用法,包括局部变量的声明和`setState`方法更新UI。还展示了如何将状态拆分到自定义组件中进行数据交互,以及实现新增列表数据的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.text 使用默认使用系统标题 Theme.of(context).textTheme.titleSmall

// 使用系统样式
  Text(
  '默认使用系统的标题样式',
  style: Theme.of(context).textTheme.titleSmall,
),
const Text(
  '使用自定义标题样式',
  style: TextStyle(
    fontSize: 18,
    fontWeight: FontWeight.bold,
  ),
),

在这里插入图片描述

2.有状态组件基础使用


class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
  
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _count = 0;// 定义局部变量可以在 state 中使用更改

  
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                '$_count',
                style: Theme.of(context).textTheme.headline1,
              ),
              ElevatedButton.icon(
                onPressed: () {
                  setState(() {
                    _count++; // 修改值
                  });
                },
                icon: const Icon(Icons.add),
                label: const Text('增加'),
              ),
            ],
          ),
        ));
  }
}

在这里插入图片描述在这里插入图片描述

3.state 拆分多个自定义组件,数据交互;

import 'package:flutter/material.dart';

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

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

  
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _count = 0;
  // 自定义函数增加_count!
  _addCount() {
    setState(() {
      _count++;
    });
  }

  
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.green),
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.green[200],
          onPressed: () {},
          child: IconButton(
            onPressed: _addCount,
            icon: const Icon(Icons.add),
          ),
        ),
        appBar: AppBar(
          title: const Text('Flutter之state有状态组件的使用'),
        ),
        body: HomePage(_count, onclick: _addCount),
      ),
    );
  }
}

// ignore: must_be_immutable
class HomePage extends StatefulWidget {
  final int _count;
  void Function()? onclick; // 自定义组件接参方法
  HomePage(this._count, {Key? key, this.onclick}) : super(key: key);
  
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                '${widget._count}',
                style: Theme.of(context).textTheme.headline1,
              ),
              ElevatedButton.icon(
                onPressed: widget.onclick,
                icon: const Icon(Icons.add),
                label: const Text('增加'),
              ),
            ],
          ),
        ));
  }
}

在这里插入图片描述

3.实现新增列表数据;

import 'package:flutter/material.dart';

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

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

  
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List _list = []; // 自定义全局列表
  // 自定义方法实现增加
  _addList() {
    _list.add('我是第${_list.length + 1}条数据');
    setState(() {
      _list = _list;
    });
  }

  
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.green),
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.green[200],
          onPressed: () {},
          child: IconButton(
            onPressed: _addList,
            icon: const Icon(Icons.add),
          ),
        ),
        appBar: AppBar(
          title: const Text('Flutter之state有状态组件的使用'),
        ),
        body: HomePage(_list),
      ),
    );
  }
}

// ignore: must_be_immutable
class HomePage extends StatefulWidget {
  final List _list;
  const HomePage(this._list, {Key? key}) : super(key: key);
  
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  
  Widget build(BuildContext context) {
    return ListView(
      children: widget._list.map((item) {
        return ListTile(title: Text('$item'));
      }).toList(),
    );
  }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值