如何制作动态文本字段
How can I make dynamic text fields
我正在开发数学应用程序。目前我正在研究矩阵。我想要当用户矩阵的 select 维度时,然后在下一个屏幕上出现具有相同维度的文本字段。
我如何处理每个文本字段的 TextEditing 控制器?
Here is how I'm taking matrix dimension from user
我制作了一个简单的文本字段来接收用户的输入
class Matrice extends StatelessWidget {
Matrice({Key? key}) : super(key: key);
final controller = Get.find<MatricesController>();
@override
Widget build(BuildContext context) {
return Container(
// color: Colors.green,
// height: 50.0,
width: double.infinity,
child: Row(
children: [
Text(
'[',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
MathsField(
controller: controller.a11,
),
SizedBox(
width: 15.0,
),
MathsField(
controller: controller.a12,
),
Text(
']',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
Text(
'+',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
Text(
'[',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
MathsField(
controller: controller.a21,
),
SizedBox(
width: 15.0,
),
MathsField(
controller: controller.a22,
),
Text(
']',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
],
),
);
}
}
The output looks like this
并为每个文本字段制作控制器
TextEditingController a11 = TextEditingController();
TextEditingController a12 = TextEditingController();
TextEditingController a21 = TextEditingController();
TextEditingController a22 = TextEditingController();
谢谢:)
我做了一个简单的例子,你可以在 DartPad 上查看:Matrix Example
简单地说,您需要为 Matrix 的每个单元格编写带有控制器的文本字段创建逻辑。这将转化为基于 Matrix
自定义定义的 List<List<TextEditingController>>
(即具有行数和列数)。
我在您的示例中看到您正在其他地方(在小部件树之外)创建控制器。我不确定这是否是您想要采用的方法。最好管理 Matrix 数据,因为这是您感兴趣的内容。根据手头的数据,您可以重建矩阵,如下所示 MatrixPage
。这也将允许您保存数据并在其他地方使用它(请参阅:页面上的 printMatrix
方法,您可以执行相同的操作来导出数据)。
参考代码:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo Home Page'),
),
body: const 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 Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
MatrixOption(matrix: Matrix(1, 1)),
MatrixOption(matrix: Matrix(2, 2)),
MatrixOption(matrix: Matrix(3, 3)),
],
),
);
}
}
class Matrix {
final int rows;
final int columns;
const Matrix(this.rows, this.columns);
}
class MatrixOption extends StatelessWidget {
final Matrix matrix;
const MatrixOption({
Key? key,
required this.matrix,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return Scaffold(
appBar: AppBar(),
body: MatrixPage(matrix: matrix),
);
},
),
);
},
child: Container(
height: 50,
width: 100,
margin: const EdgeInsets.all(8),
color: Colors.orange,
child: Center(child: Text('${matrix.rows}' 'x' '${matrix.columns} Matrix ')),
),
);
}
}
class MatrixPage extends StatefulWidget {
final Matrix matrix;
const MatrixPage({
Key? key,
required this.matrix,
}) : super(key: key);
@override
State<MatrixPage> createState() => _MatrixPageState();
}
class _MatrixPageState extends State<MatrixPage> {
// List of lists (outer list is the rows, inner list is the columns)
final controllers = <List<TextEditingController>>[];
late final rows = widget.matrix.rows;
late final columns = widget.matrix.columns;
@override
void initState() {
super.initState();
createControllers();
}
void createControllers() {
for (var i = 0; i < rows; i++) {
controllers.add(List.generate(columns, (index) => TextEditingController(text: '0')));
}
}
void printMatrix() {
final strings = <List<String>>[];
for (var controllerRow in controllers) {
final row = controllerRow.map((e) => e.text).toList();
strings.add(row);
}
print(strings);
}
@override
void dispose() {
for (var controllerRow in controllers) {
for (final c in controllerRow) {
c.dispose();
}
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
controllers.length,
(index1) => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
controllers[index1].length,
(index2) => Center(
child: MatrixField(
controller: controllers[index1][index2],
),
),
),
),
),
),
TextButton(
onPressed: printMatrix,
child: const Text('Print Matrix'),
)
],
),
);
}
}
class MatrixField extends StatelessWidget {
final TextEditingController controller;
const MatrixField({
Key? key,
required this.controller,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
height: 50,
width: 50,
child: TextField(
controller: controller,
),
);
}
}
如果您对代码有任何疑问,请告诉我。
我正在开发数学应用程序。目前我正在研究矩阵。我想要当用户矩阵的 select 维度时,然后在下一个屏幕上出现具有相同维度的文本字段。
我如何处理每个文本字段的 TextEditing 控制器?
Here is how I'm taking matrix dimension from user
我制作了一个简单的文本字段来接收用户的输入
class Matrice extends StatelessWidget {
Matrice({Key? key}) : super(key: key);
final controller = Get.find<MatricesController>();
@override
Widget build(BuildContext context) {
return Container(
// color: Colors.green,
// height: 50.0,
width: double.infinity,
child: Row(
children: [
Text(
'[',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
MathsField(
controller: controller.a11,
),
SizedBox(
width: 15.0,
),
MathsField(
controller: controller.a12,
),
Text(
']',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
Text(
'+',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
Text(
'[',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
MathsField(
controller: controller.a21,
),
SizedBox(
width: 15.0,
),
MathsField(
controller: controller.a22,
),
Text(
']',
style: AppTextStyle.kBlackBold
.copyWith(fontSize: 80.sp, fontWeight: FontWeight.w300),
),
],
),
);
}
}
The output looks like this
并为每个文本字段制作控制器
TextEditingController a11 = TextEditingController();
TextEditingController a12 = TextEditingController();
TextEditingController a21 = TextEditingController();
TextEditingController a22 = TextEditingController();
谢谢:)
我做了一个简单的例子,你可以在 DartPad 上查看:Matrix Example
简单地说,您需要为 Matrix 的每个单元格编写带有控制器的文本字段创建逻辑。这将转化为基于 Matrix
自定义定义的 List<List<TextEditingController>>
(即具有行数和列数)。
我在您的示例中看到您正在其他地方(在小部件树之外)创建控制器。我不确定这是否是您想要采用的方法。最好管理 Matrix 数据,因为这是您感兴趣的内容。根据手头的数据,您可以重建矩阵,如下所示 MatrixPage
。这也将允许您保存数据并在其他地方使用它(请参阅:页面上的 printMatrix
方法,您可以执行相同的操作来导出数据)。
参考代码:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo Home Page'),
),
body: const 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 Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
MatrixOption(matrix: Matrix(1, 1)),
MatrixOption(matrix: Matrix(2, 2)),
MatrixOption(matrix: Matrix(3, 3)),
],
),
);
}
}
class Matrix {
final int rows;
final int columns;
const Matrix(this.rows, this.columns);
}
class MatrixOption extends StatelessWidget {
final Matrix matrix;
const MatrixOption({
Key? key,
required this.matrix,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return Scaffold(
appBar: AppBar(),
body: MatrixPage(matrix: matrix),
);
},
),
);
},
child: Container(
height: 50,
width: 100,
margin: const EdgeInsets.all(8),
color: Colors.orange,
child: Center(child: Text('${matrix.rows}' 'x' '${matrix.columns} Matrix ')),
),
);
}
}
class MatrixPage extends StatefulWidget {
final Matrix matrix;
const MatrixPage({
Key? key,
required this.matrix,
}) : super(key: key);
@override
State<MatrixPage> createState() => _MatrixPageState();
}
class _MatrixPageState extends State<MatrixPage> {
// List of lists (outer list is the rows, inner list is the columns)
final controllers = <List<TextEditingController>>[];
late final rows = widget.matrix.rows;
late final columns = widget.matrix.columns;
@override
void initState() {
super.initState();
createControllers();
}
void createControllers() {
for (var i = 0; i < rows; i++) {
controllers.add(List.generate(columns, (index) => TextEditingController(text: '0')));
}
}
void printMatrix() {
final strings = <List<String>>[];
for (var controllerRow in controllers) {
final row = controllerRow.map((e) => e.text).toList();
strings.add(row);
}
print(strings);
}
@override
void dispose() {
for (var controllerRow in controllers) {
for (final c in controllerRow) {
c.dispose();
}
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
controllers.length,
(index1) => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
controllers[index1].length,
(index2) => Center(
child: MatrixField(
controller: controllers[index1][index2],
),
),
),
),
),
),
TextButton(
onPressed: printMatrix,
child: const Text('Print Matrix'),
)
],
),
);
}
}
class MatrixField extends StatelessWidget {
final TextEditingController controller;
const MatrixField({
Key? key,
required this.controller,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
height: 50,
width: 50,
child: TextField(
controller: controller,
),
);
}
}
如果您对代码有任何疑问,请告诉我。