Flutter 状态的问题
Issues with Flutter state
这是 Flutter 的实验。我试图在 Flutter 中创建一个段落生成器。做了一个UI
已从 List<String>
.
加载数据
data.dart
class MyData {
static final List<String> data1 = [
'0Title Of Page',
'3Paragraph title One',
'1First Paragraph Content Of Page',
'3Paragraph title two',
'1Second Paragraph Content Of Page',
'4End of the page line',
];
}
我想将此列表中的第一项添加到 AppBar()
的标题中,结果成功了。然后我想从此列表中删除第一项(用作 AppBar
标题)并继续处理列表中的其余项以构建 [=74= 的 body:
].
当我们第一次打开这个页面时,这工作正常。从下一次开始,每次打开和关闭页面时,我们都会丢失列表中的第一项。尽管页面是 StatelessWidget
,但我们丢失了项目并最终列出了 运行 个项目。
我试过的东西:
- 我尝试将页面和小部件更改为
StatelessWidget
。
- 尝试
override
AppBar
后退按钮和 pop()
页面。
- 已复制
List<String>
到新变量。
- 使用了
dispose()
方法。
还是一样。可能是什么问题?添加下面的代码。
main.dart
import 'package:flutter/material.dart';
import 'home_page.dart';
import 'page_one.dart';
import 'page_two.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Paragraph Test',
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/pageone': (context) => PageOne(),
'/pagetwo': (context) => PageTwo(),
},
);
}
}
home_page.dart
import 'package:flutter/material.dart';
import 'button.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page Test'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
CButton(
onTap: () {
Navigator.pushNamed(context, '/pageone');
},
text: 'Page One',
),
CButton(
onTap: () {
Navigator.pushNamed(context, '/pagetwo');
},
text: 'Page Two',
),
],
),
);
}
}
page_one.dart
import 'package:flutter/material.dart';
import 'data.dart';
var weight = FontWeight.normal;
var align = TextAlign.center;
var color = Colors.white60;
var size = 16.0;
String x, y;
class PageOne extends StatelessWidget {
buildText(String b) {
x = b.substring(0, 1);
y = b.substring(1);
if (x.contains('0')) {
weight = FontWeight.bold;
color = Colors.blue;
size = 18.0;
align = TextAlign.center;
} else if (x.contains('2')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.right;
size = 16.0;
} else if (x.contains('3')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.center;
size = 16.0;
} else if (x.contains('4')) {
weight = FontWeight.normal;
color = Colors.red;
align = TextAlign.center;
size = 16.0;
} else {
weight = FontWeight.normal;
color = Colors.white60;
align = TextAlign.center;
size = 16.0;
}
return y;
}
@override
Widget build(BuildContext context) {
String title = MyData.data1.first.substring(1);
MyData.data1.removeAt(0);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('$title'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
),
child: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.all(
12.0,
),
decoration: BoxDecoration(
color: Color(0x99000000),
borderRadius: BorderRadius.circular(10.0),
),
child: SingleChildScrollView(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: MyData.data1
.map(
(item) => Text(
buildText(item),
textAlign: align,
style: TextStyle(
color: color,
fontWeight: weight,
fontSize: size,
),
),
)
.toList(),
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Text('Page 1'),
onPressed: () {},
),
);
}
}
page_two.dart
import 'package:flutter/material.dart';
import 'data.dart';
var weight = FontWeight.normal;
var align = TextAlign.center;
var color = Colors.white60;
var size = 16.0;
String x, y;
class PageTwo extends StatelessWidget {
buildText(String b) {
x = b.substring(0, 1);
y = b.substring(1);
if (x.contains('0')) {
weight = FontWeight.bold;
color = Colors.blue;
size = 18.0;
align = TextAlign.center;
} else if (x.contains('2')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.right;
size = 16.0;
} else if (x.contains('3')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.center;
size = 16.0;
} else if (x.contains('4')) {
weight = FontWeight.normal;
color = Colors.red;
align = TextAlign.center;
size = 16.0;
} else {
weight = FontWeight.normal;
color = Colors.white60;
align = TextAlign.center;
size = 16.0;
}
return y;
}
@override
Widget build(BuildContext context) {
String title = MyData.data1.first.substring(1);
MyData.data1.removeAt(0);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('$title'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
),
child: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.all(
12.0,
),
decoration: BoxDecoration(
color: Color(0x99000000),
borderRadius: BorderRadius.circular(10.0),
),
child: SingleChildScrollView(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: MyData.data1
.map(
(item) => Text(
buildText(item),
textAlign: align,
style: TextStyle(
color: color,
fontWeight: weight,
fontSize: size,
),
),
)
.toList(),
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Text('Page 2'),
onPressed: () {},
),
);
}
}
button.dart
import 'package:flutter/material.dart';
class CButton extends StatelessWidget {
CButton({this.text, this.onTap});
final String text;
final Function onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
margin: EdgeInsets.all(2.0),
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(color: Colors.blue),
child: Text(
'$text',
style: TextStyle(color: Colors.white),
),
),
);
}
}
data1
是 static
MyData
class 中的变量。所以它是一种带有命名空间 MyData
的全局变量。所以每次你做 MyData.data1.removeAt(0);
都会影响相同的内存(或对象)。
对于你的情况试试这个,
class MyData {
static List<String> get data1 => [
'0Title Of Page',
'3Paragraph title One',
'1First Paragraph Content Of Page',
'3Paragraph title two',
'1Second Paragraph Content Of Page',
'4End of the page line',
];
}
class PageOne extends StatelessWidget {
buildText(String b) {
x = b.substring(0, 1);
y = b.substring(1);
if (x.contains('0')) {
weight = FontWeight.bold;
color = Colors.blue;
size = 18.0;
align = TextAlign.center;
} else if (x.contains('2')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.right;
size = 16.0;
} else if (x.contains('3')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.center;
size = 16.0;
} else if (x.contains('4')) {
weight = FontWeight.normal;
color = Colors.red;
align = TextAlign.center;
size = 16.0;
} else {
weight = FontWeight.normal;
color = Colors.white60;
align = TextAlign.center;
size = 16.0;
}
return y;
}
@override
Widget build(BuildContext context) {
final data1 = MyData.data1;
String title = data1.first.substring(1);
data1.removeAt(0);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('$title'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
),
child: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.all(
12.0,
),
decoration: BoxDecoration(
color: Color(0x99000000),
borderRadius: BorderRadius.circular(10.0),
),
child: SingleChildScrollView(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: data1
.map(
(item) => Text(
buildText(item),
textAlign: align,
style: TextStyle(
color: color,
fontWeight: weight,
fontSize: size,
),
),
)
.toList(),
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Text('Page 1'),
onPressed: () {},
),
);
}
}
这是 Flutter 的实验。我试图在 Flutter 中创建一个段落生成器。做了一个UI
已从 List<String>
.
data.dart
class MyData {
static final List<String> data1 = [
'0Title Of Page',
'3Paragraph title One',
'1First Paragraph Content Of Page',
'3Paragraph title two',
'1Second Paragraph Content Of Page',
'4End of the page line',
];
}
我想将此列表中的第一项添加到 AppBar()
的标题中,结果成功了。然后我想从此列表中删除第一项(用作 AppBar
标题)并继续处理列表中的其余项以构建 [=74= 的 body:
].
当我们第一次打开这个页面时,这工作正常。从下一次开始,每次打开和关闭页面时,我们都会丢失列表中的第一项。尽管页面是 StatelessWidget
,但我们丢失了项目并最终列出了 运行 个项目。
我试过的东西:
- 我尝试将页面和小部件更改为
StatelessWidget
。 - 尝试
override
AppBar
后退按钮和pop()
页面。 - 已复制
List<String>
到新变量。 - 使用了
dispose()
方法。
还是一样。可能是什么问题?添加下面的代码。
main.dart
import 'package:flutter/material.dart';
import 'home_page.dart';
import 'page_one.dart';
import 'page_two.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Paragraph Test',
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/pageone': (context) => PageOne(),
'/pagetwo': (context) => PageTwo(),
},
);
}
}
home_page.dart
import 'package:flutter/material.dart';
import 'button.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page Test'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
CButton(
onTap: () {
Navigator.pushNamed(context, '/pageone');
},
text: 'Page One',
),
CButton(
onTap: () {
Navigator.pushNamed(context, '/pagetwo');
},
text: 'Page Two',
),
],
),
);
}
}
page_one.dart
import 'package:flutter/material.dart';
import 'data.dart';
var weight = FontWeight.normal;
var align = TextAlign.center;
var color = Colors.white60;
var size = 16.0;
String x, y;
class PageOne extends StatelessWidget {
buildText(String b) {
x = b.substring(0, 1);
y = b.substring(1);
if (x.contains('0')) {
weight = FontWeight.bold;
color = Colors.blue;
size = 18.0;
align = TextAlign.center;
} else if (x.contains('2')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.right;
size = 16.0;
} else if (x.contains('3')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.center;
size = 16.0;
} else if (x.contains('4')) {
weight = FontWeight.normal;
color = Colors.red;
align = TextAlign.center;
size = 16.0;
} else {
weight = FontWeight.normal;
color = Colors.white60;
align = TextAlign.center;
size = 16.0;
}
return y;
}
@override
Widget build(BuildContext context) {
String title = MyData.data1.first.substring(1);
MyData.data1.removeAt(0);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('$title'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
),
child: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.all(
12.0,
),
decoration: BoxDecoration(
color: Color(0x99000000),
borderRadius: BorderRadius.circular(10.0),
),
child: SingleChildScrollView(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: MyData.data1
.map(
(item) => Text(
buildText(item),
textAlign: align,
style: TextStyle(
color: color,
fontWeight: weight,
fontSize: size,
),
),
)
.toList(),
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Text('Page 1'),
onPressed: () {},
),
);
}
}
page_two.dart
import 'package:flutter/material.dart';
import 'data.dart';
var weight = FontWeight.normal;
var align = TextAlign.center;
var color = Colors.white60;
var size = 16.0;
String x, y;
class PageTwo extends StatelessWidget {
buildText(String b) {
x = b.substring(0, 1);
y = b.substring(1);
if (x.contains('0')) {
weight = FontWeight.bold;
color = Colors.blue;
size = 18.0;
align = TextAlign.center;
} else if (x.contains('2')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.right;
size = 16.0;
} else if (x.contains('3')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.center;
size = 16.0;
} else if (x.contains('4')) {
weight = FontWeight.normal;
color = Colors.red;
align = TextAlign.center;
size = 16.0;
} else {
weight = FontWeight.normal;
color = Colors.white60;
align = TextAlign.center;
size = 16.0;
}
return y;
}
@override
Widget build(BuildContext context) {
String title = MyData.data1.first.substring(1);
MyData.data1.removeAt(0);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('$title'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
),
child: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.all(
12.0,
),
decoration: BoxDecoration(
color: Color(0x99000000),
borderRadius: BorderRadius.circular(10.0),
),
child: SingleChildScrollView(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: MyData.data1
.map(
(item) => Text(
buildText(item),
textAlign: align,
style: TextStyle(
color: color,
fontWeight: weight,
fontSize: size,
),
),
)
.toList(),
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Text('Page 2'),
onPressed: () {},
),
);
}
}
button.dart
import 'package:flutter/material.dart';
class CButton extends StatelessWidget {
CButton({this.text, this.onTap});
final String text;
final Function onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
margin: EdgeInsets.all(2.0),
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(color: Colors.blue),
child: Text(
'$text',
style: TextStyle(color: Colors.white),
),
),
);
}
}
data1
是 static
MyData
class 中的变量。所以它是一种带有命名空间 MyData
的全局变量。所以每次你做 MyData.data1.removeAt(0);
都会影响相同的内存(或对象)。
对于你的情况试试这个,
class MyData {
static List<String> get data1 => [
'0Title Of Page',
'3Paragraph title One',
'1First Paragraph Content Of Page',
'3Paragraph title two',
'1Second Paragraph Content Of Page',
'4End of the page line',
];
}
class PageOne extends StatelessWidget {
buildText(String b) {
x = b.substring(0, 1);
y = b.substring(1);
if (x.contains('0')) {
weight = FontWeight.bold;
color = Colors.blue;
size = 18.0;
align = TextAlign.center;
} else if (x.contains('2')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.right;
size = 16.0;
} else if (x.contains('3')) {
weight = FontWeight.normal;
color = Colors.blue;
align = TextAlign.center;
size = 16.0;
} else if (x.contains('4')) {
weight = FontWeight.normal;
color = Colors.red;
align = TextAlign.center;
size = 16.0;
} else {
weight = FontWeight.normal;
color = Colors.white60;
align = TextAlign.center;
size = 16.0;
}
return y;
}
@override
Widget build(BuildContext context) {
final data1 = MyData.data1;
String title = data1.first.substring(1);
data1.removeAt(0);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('$title'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
),
child: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.all(
12.0,
),
decoration: BoxDecoration(
color: Color(0x99000000),
borderRadius: BorderRadius.circular(10.0),
),
child: SingleChildScrollView(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: data1
.map(
(item) => Text(
buildText(item),
textAlign: align,
style: TextStyle(
color: color,
fontWeight: weight,
fontSize: size,
),
),
)
.toList(),
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Text('Page 1'),
onPressed: () {},
),
);
}
}