forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[go_router] Adds an ability to add a custom codec for serializing/des… (
flutter#5288) �erializing extra fixes flutter#99099 fixes flutter#137248
- Loading branch information
Showing
14 changed files
with
404 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/go_router/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
/// This sample app demonstrates how to provide a codec for complex extra data. | ||
void main() => runApp(const MyApp()); | ||
|
||
/// The router configuration. | ||
final GoRouter _router = GoRouter( | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: '/', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const HomeScreen(), | ||
), | ||
], | ||
extraCodec: const MyExtraCodec(), | ||
); | ||
|
||
/// The main app. | ||
class MyApp extends StatelessWidget { | ||
/// Constructs a [MyApp] | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router( | ||
routerConfig: _router, | ||
); | ||
} | ||
} | ||
|
||
/// The home screen. | ||
class HomeScreen extends StatelessWidget { | ||
/// Constructs a [HomeScreen]. | ||
const HomeScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Home Screen')), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
const Text( | ||
"If running in web, use the browser's backward and forward button to test extra codec after setting extra several times."), | ||
Text( | ||
'The extra for this page is: ${GoRouterState.of(context).extra}'), | ||
ElevatedButton( | ||
onPressed: () => context.go('/', extra: ComplexData1('data')), | ||
child: const Text('Set extra to ComplexData1'), | ||
), | ||
ElevatedButton( | ||
onPressed: () => context.go('/', extra: ComplexData2('data')), | ||
child: const Text('Set extra to ComplexData2'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// A complex class. | ||
class ComplexData1 { | ||
/// Create a complex object. | ||
ComplexData1(this.data); | ||
|
||
/// The data. | ||
final String data; | ||
|
||
@override | ||
String toString() => 'ComplexData1(data: $data)'; | ||
} | ||
|
||
/// A complex class. | ||
class ComplexData2 { | ||
/// Create a complex object. | ||
ComplexData2(this.data); | ||
|
||
/// The data. | ||
final String data; | ||
|
||
@override | ||
String toString() => 'ComplexData2(data: $data)'; | ||
} | ||
|
||
/// A codec that can serialize both [ComplexData1] and [ComplexData2]. | ||
class MyExtraCodec extends Codec<Object?, Object?> { | ||
/// Create a codec. | ||
const MyExtraCodec(); | ||
@override | ||
Converter<Object?, Object?> get decoder => const _MyExtraDecoder(); | ||
|
||
@override | ||
Converter<Object?, Object?> get encoder => const _MyExtraEncoder(); | ||
} | ||
|
||
class _MyExtraDecoder extends Converter<Object?, Object?> { | ||
const _MyExtraDecoder(); | ||
@override | ||
Object? convert(Object? input) { | ||
if (input == null) { | ||
return null; | ||
} | ||
final List<Object?> inputAsList = input as List<Object?>; | ||
if (inputAsList[0] == 'ComplexData1') { | ||
return ComplexData1(inputAsList[1]! as String); | ||
} | ||
if (inputAsList[0] == 'ComplexData2') { | ||
return ComplexData2(inputAsList[1]! as String); | ||
} | ||
throw FormatException('Unable tp parse input: $input'); | ||
} | ||
} | ||
|
||
class _MyExtraEncoder extends Converter<Object?, Object?> { | ||
const _MyExtraEncoder(); | ||
@override | ||
Object? convert(Object? input) { | ||
if (input == null) { | ||
return null; | ||
} | ||
switch (input.runtimeType) { | ||
case ComplexData1: | ||
return <Object?>['ComplexData1', (input as ComplexData1).data]; | ||
case ComplexData2: | ||
return <Object?>['ComplexData2', (input as ComplexData2).data]; | ||
default: | ||
throw FormatException('Cannot encode type ${input.runtimeType}'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router_examples/extra_codec.dart' as example; | ||
|
||
void main() { | ||
testWidgets('example works', (WidgetTester tester) async { | ||
await tester.pumpWidget(const example.MyApp()); | ||
expect(find.text('The extra for this page is: null'), findsOneWidget); | ||
|
||
await tester.tap(find.text('Set extra to ComplexData1')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('The extra for this page is: ComplexData1(data: data)'), | ||
findsOneWidget); | ||
|
||
await tester.tap(find.text('Set extra to ComplexData2')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('The extra for this page is: ComplexData2(data: data)'), | ||
findsOneWidget); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.