Skip to content

Commit

Permalink
BuildPage的链式传递,ok,貌似还能简化
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed May 3, 2024
1 parent 2e807d5 commit dd529f1
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 237 deletions.
3 changes: 1 addition & 2 deletions notes/flutter_web/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class App extends StatelessWidget {

@override
Widget build(BuildContext context) {
debugPrint("user base ${Uri.base}");

debugPrint("App: Uri.base = ${Uri.base}");
return MaterialApp.router(
title: "flutter note",
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.tealAccent.shade700, brightness: Brightness.light), useMaterial3: true),
Expand Down
3 changes: 2 additions & 1 deletion notes/flutter_web/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void main() async {
sharedPreferences: await SharedPreferences.getInstance(),
router: YouRouter(
root: routes.root,
initial: routes.routes_root.toUri(),
initial: routes.routes_notes.toUri(),
// initial: Uri.base,
navigatorKey: GlobalKey<NavigatorState>(debugLabel: "mainNavigator"),
),
));
Expand Down
7 changes: 4 additions & 3 deletions notes/flutter_web/lib/routes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:you_flutter/router.dart';
import 'package:flutter_web/routes/page.dart';
import 'package:flutter_web/routes/layout.dart';
import 'package:you_flutter/note.dart';
import 'package:flutter_web/routes/notes/page.dart' as _notes_page;
import 'package:flutter_web/routes/notes/layout.dart';
import 'package:flutter_web/routes/notes/layout.dart' as _notes_layout;
import 'package:flutter_web/routes/notes/research/parameterized/page.dart' as _parameterized_page;
import 'package:flutter_web/routes/notes/research/remote_view/page.dart' as _remote_view_page;
import 'package:flutter_web/routes/notes/env_info/page.dart' as _env_info_page;
Expand Down Expand Up @@ -66,8 +67,8 @@ import 'package:flutter_web/routes/notes/Improve_app/RepaintBoundary/page.dart'
import 'package:flutter_web/routes/notes/Improve_app/event&listener&lifeycle/page.dart' as _event_listener_lifeycle_page;

mixin RoutesMixin {
final RouteNode root = ToPage('routes', page: build).route(children: [
ToNote('notes', page: _notes_page.build, layout: layout).route(children: [
final RouteNode root = ToPage('routes', page: build, layout: layout).route(children: [
ToNote('notes', page: _notes_page.build, layout: _notes_layout.layout).route(children: [
ToNote('research').route(children: [
ToNote('bash_note').route(),
ToNote('parameterized', page: _parameterized_page.build).route(),
Expand Down
37 changes: 37 additions & 0 deletions notes/flutter_web/lib/routes/layout.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:you_flutter/router.dart';
import 'package:you_flutter/note.dart';

/// [NoteLayoutBuilder]
BuildResult layout(BuildContext context, BuildResult child) {
// ignore: unnecessary_type_check
assert(layout is PageLayoutBuilder);
return child.warp(RootLayout(child: child));
}

class RootLayout extends StatelessWidget {
final BuildResult child;

const RootLayout({super.key, required this.child});

@override
Widget build(BuildContext context) {
final route = YouRouter.of(context);


return Scaffold(
primary: true,
// content...
appBar: AppBar(toolbarHeight: 38, title: Text("location: ${route.uri}"), actions: [
IconButton(iconSize: 24, icon: const Icon(Icons.color_lens_outlined), onPressed: () {}),
IconButton(iconSize: 24, icon: const Icon(Icons.settings), onPressed: () {}),
]),
body: SafeArea(
child: SelectionArea(
child: child.widget,
),
),
);
}
}
76 changes: 73 additions & 3 deletions notes/flutter_web/lib/routes/notes/layout.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:you_flutter/router.dart';
import 'package:you_flutter/note.dart';
import 'package:you_flutter/state.dart';

/// [NoteLayoutBuilder]
@ToType(type: ToNote)
Widget layout(BuildContext context, NoteBuilder builder) {
BuildNote layout(BuildContext context, BuildNote child) {
// ignore: unnecessary_type_check
assert(layout is NoteLayoutBuilder);
return NoteLayoutStyle1(builder: builder);
return child.warp(NoteLayout(child: child));
}


/// 一个极简的笔记布局范例
/// 左边routes树,右边页面内容
final class NoteLayout extends StatelessWidget {
final BuildNote child;

const NoteLayout({super.key, required this.child});

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SelectionArea(
/// Watch是you_flutter的state管理组件, 被其包裹的状态可以被观测刷新(ref: Cell._contents = [].signal())
child: Watch((context) {
// contents是收集到调用print(xx)的所有结果
var pageContents = child.cell.toList().expand((cell) sync* {
for (var content in cell.contents) {
yield Align(
alignment: Alignment.centerLeft,
child: contents.contentToWidget(content),
);
}
}).toList();
return Row(
children: [
// IntrinsicWidth(child: _NoteTreeView(uri)),
const _RouteTree(),
Flexible(child: ListView(children: pageContents)),
],
);
}),
),
),
);
}
}

class _RouteTree extends StatelessWidget {
const _RouteTree();

@override
Widget build(BuildContext context) {
final route = YouRouter.of(context);

var validRoutes = route.root.toList().where((e) => e.isValid || e.isNonLeaf);
var routeWidgets = validRoutes.map((node) {
String title = "▼ ${node.part}";
title = title.padLeft((node.level * 3) + title.length);

click() {
route.to(node.toUri());
}

return Align(
alignment: Alignment.centerLeft,
child: TextButton(onPressed: node.isValid ? click : null, child: Text(title)),
);
});
return ConstrainedBox(
constraints: const BoxConstraints.tightFor(width: 350),
child: ListView(
children: [
...routeWidgets,
],
));
}
}
3 changes: 3 additions & 0 deletions notes/flutter_web/lib/routes/page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:you_flutter/router.dart';

RootPage build(BuildContext context) {
// ignore: unnecessary_type_check
assert(build is PageBuilder);
return const RootPage();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/you_cli/lib/src/cli_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class YouCli {
: dir_project = projectDir.fileSystem.directory(path.normalize(path.absolute(projectDir.path))),
fs = projectDir.fileSystem;

static const Reference toType = Reference("To", "package:you_flutter/router.dart");
static const Reference forPageType = Reference("ForPage", "package:you_flutter/router.dart");
static const Reference toType = Reference("RouteNode", "package:you_flutter/router.dart");
static const Reference forPageType = Reference("ToPage", "package:you_flutter/router.dart");
static const String toTypeName = "ToType";
static const String pageDart = "page.dart";
static const String layoutDart = "layout.dart";
Expand Down
4 changes: 1 addition & 3 deletions packages/you_flutter/lib/note.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
library note;

export 'src/note/note.dart' show ToNote, Cell, NoteBuilder, NoteLayoutBuilder, NoteSystem;
export 'src/note/note.dart' show ToNote, Cell, NoteBuilder,BuildNote, NoteLayoutBuilder, NoteSystem;

export 'src/note/contents/contents.dart' show contents, Contents;
export 'src/note/contents/mockup.dart' show MockupWindow;
export 'src/note/contents/markdown_content.dart' show MD;

export 'src/note/layouts/note_layout_style_1.dart' show NoteLayoutStyle1;
export 'src/note/layouts/note_layout_default.dart' show NoteLayoutDefault;
2 changes: 1 addition & 1 deletion packages/you_flutter/lib/router.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library you_router;

export 'package:you_flutter/src/router.dart' show YouRouter, RouteNode, ToType, RouteUri, RouteBuilder, ToPage, PageBuilder, LazyPageBuilder, PageLayoutBuilder;
export 'package:you_flutter/src/router.dart' show YouRouter, RouteNode,BuildResult, ToType, RouteUri, RouteBuilder, ToPage, PageBuilder, LazyPageBuilder, PageLayoutBuilder;
80 changes: 0 additions & 80 deletions packages/you_flutter/lib/src/layouts/layout_default.dart

This file was deleted.

36 changes: 0 additions & 36 deletions packages/you_flutter/lib/src/note/layouts/note_layout_default.dart

This file was deleted.

75 changes: 0 additions & 75 deletions packages/you_flutter/lib/src/note/layouts/note_layout_style_1.dart

This file was deleted.

Loading

0 comments on commit dd529f1

Please sign in to comment.