import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SecondPage extends StatefulWidget {
const SecondPage({Key? key}) : super(key: key);
@override
State<SecondPage> createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
// ️ 页面UI构建入口
return Scaffold(
appBar: AppBar(
title: const Text("积极日记",
textDirection: TextDirection.ltr,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontFamily: 'Comic Sans MS',
fontSize: 20.0,
fontWeight: FontWeight.w500,
)),
centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0,
surfaceTintColor: Colors.transparent, // 避免表面色调影响
flexibleSpace: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/background.jpg'), // 图片路径
fit: BoxFit.cover, // 覆盖整个区域
),
),
),
// 返回按钮实现
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.pop(context),
),
),
body: Container(
width: 430,
height: 900,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/background.jpg'), // 图片路径
fit: BoxFit.cover, // 覆盖整个区域
),
),
child: InputSaverContainer(),
),
);
}
}
class InputSaverContainer extends StatefulWidget {
const InputSaverContainer({super.key});
@override
_InputSaverContainerState createState() => _InputSaverContainerState();
}
class _InputSaverContainerState extends State<InputSaverContainer> {
final TextEditingController _controller = TextEditingController();
List<String> _savedTexts = []; // 改为列表存储多个内容
Future<void> _saveInput() async {
if (_controller.text.isEmpty) return;
final prefs = await SharedPreferences.getInstance();
// 获取已有列表并添加新内容
//final List<String> existingList = prefs.getStringList(_storageKey) ?? [];
//existingList.add(_controller.text);
_savedTexts.insert(0, _controller.text);
// 保存更新后的列表
//await prefs.setStringList(_storageKey, existingList);
await prefs.setStringList('saved_items', _savedTexts);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("保存成功!"), duration: Duration(seconds: 1)));
_controller.clear(); // 清空输入框
setState(() {});
}
Future<void> _loadInputs() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_savedTexts = prefs.getStringList('saved_items') ?? [];
});
}
// 删除指定项
Future<void> _deleteItem(int index) async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_savedTexts.removeAt(index);
prefs.setStringList('saved_items', _savedTexts);
});
}
@override
void initState() {
super.initState();
_loadInputs();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 620,
width: 500,
child: Container(
height: 20,
width: 500,
padding: const EdgeInsets.all(16),
decoration: const BoxDecoration(
color: Colors.transparent,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: _controller,
maxLines: null, // 关键参数:允许无限多行(自动扩展)
minLines: 15, // 初始最小显示行数
keyboardType: TextInputType.multiline, // 启用多行键盘
textInputAction: TextInputAction.newline, // 回车键换行
decoration: const InputDecoration(
labelText: null,
hintText: '请记录今天做得很好的三件事和值得感恩的三件事 !',
/*decoration: InputDecoration(
labelText: "输入内容",
prefixIcon: Icon(Icons.edit),
border: OutlineInputBorder(),
filled: true,
fillColor: Colors.white,
),*/
),
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: _saveInput,
icon: const Icon(Icons.save),
label: const Text("保存内容"),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
backgroundColor: Colors.black45,
foregroundColor: Colors.white,
elevation: 0,
),
),
),
const SizedBox(height: 20),
// 滑动列表区域
Container(
height: 390, // 固定高度
padding: const EdgeInsets.all(12),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/background.jpg'), // 图片路径
fit: BoxFit.cover, // 覆盖整个区域
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Row(children: [
Icon(
Icons.book,
color: Colors.grey,
),
Text("日记列表:", style: TextStyle(fontSize: 15)),
SizedBox(height: 8)
]),
Expanded(
child: _savedTexts.isEmpty
? const Center(child: Text("暂无保存内容"))
: ListView.builder(
itemCount: _savedTexts.length,
itemBuilder: (context, index) {
return ListTile(
/*leading: CircleAvatar(
child: Text("${index + 1}"),
backgroundColor: Colors.transparent,
),*/
title: Text(_savedTexts[index],
textDirection: TextDirection.ltr,
style: const TextStyle(
color: Colors.black54,
fontSize: 13.0,
fontWeight: FontWeight.w400)),
trailing: IconButton(
// 添加删除按钮
icon: const Icon(Icons.delete,
color: Colors.black),
onPressed: () => _deleteItem(index),
),
);
},
scrollDirection: Axis.vertical,
),
),
],
),
),
],
),
),
);
}
}请在这段代码中加入一段代码,实现点击Textfield先检测是否登录,如果没有登陆,弹出弹窗提醒用户登录,如果登录则可以正常使用textfield。
最新发布