现在,我有一个登录页面,该页面具有许多指向不同类别的链接。这些链接将转到一堆具有相似布局的页面,但是唯一的区别是每个页面的文本内容。有没有一种方法可以为这些类型的页面创建页面界面并重用该代码,或者我必须创建20个不同的页面?谢谢
答案 0 :(得分:0)
使用Flutter真的很简单,您可以使用重复的参数(实际上就是标题和内容)来创建一个新的StatefulWidget:
import 'package:flutter/material.dart';
class CustomPage extends StatefulWidget {
final String title;
final String content;
CustomPage({
Key key,
@required this.title,
@required this.content,
}) : super(key: key);
@override
_CustomPageState createState() => _CustomPageState();
}
class _CustomPageState extends State<CustomPage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: widget.title,
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
margin: EdgeInsets.all(15),
child: Text(
widget.content,
),
),
),
);
}
}
现在,要使用它,您可以在任何地方调用新的Widget:
@override
Widget build(BuildContext context) {
return CustomPage(
title: "First Title",
content: "Long long content: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
);
}
您的最终结果将是这样的: