Сейчас у меня есть целевая страница с кучей ссылок на разные категории. Эти ссылки будут вести на кучу страниц, которые будут иметь похожие макеты, но единственная разница заключается в текстовом содержании каждой страницы. Есть ли способ сделать интерфейс страницы для этих типов страниц и повторно использовать этот код, или мне нужно сделать, скажем, 20 разных страниц? Спасибо
С 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,
),
),
),
);
}
}
Теперь, чтобы использовать его, вы вызываете новый виджет где угодно:
@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.",
);
}
Ваш окончательный результат будет примерно таким:
@JacobBarcelona, с удовольствием :) не забудьте отметить мой ответ как правильный, чтобы больше людей могли его найти и получить первыми.