Instantly share code, notes, and snippets.
Created
July 5, 2024 01:00
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save seunghwanly/244fadf7ed7a1f2f6da57742f7aa6060 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Copyright 2019 the Dart project 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/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorSchemeSeed: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
onGenerateRoute: (settings) { | |
if (settings.name == '/detail') { | |
assert( | |
settings.arguments != null && | |
settings.arguments is CounterArguments, | |
); | |
final CounterArguments arguments = | |
settings.arguments! as CounterArguments; | |
return MaterialPageRoute( | |
builder: (_) => CountDetailScreen( | |
arguments.count, | |
), | |
); | |
} | |
return null; | |
}, | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
super.key, | |
required this.title, | |
}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
void navigateToDetail( | |
BuildContext context, | |
int count, | |
) async { | |
if (!context.mounted) { | |
return; | |
} | |
return Navigator.of(context).pushNamed<void>( | |
'/detail', | |
arguments: CounterArguments(count), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
onPressed: () => navigateToDetail(context, _counter), | |
tooltip: 'To Detail', | |
child: const Icon(Icons.airplanemode_active), | |
heroTag: 'detail', | |
), | |
const SizedBox(height: 8), | |
FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
heroTag: 'add', | |
), | |
], | |
), | |
); | |
} | |
} | |
abstract class RouteArguments extends Object { | |
const RouteArguments(); | |
} | |
class CounterArguments extends RouteArguments { | |
const CounterArguments(this.count); | |
final int count; | |
} | |
class CountDetailScreen extends StatelessWidget { | |
const CountDetailScreen(this.count, {super.key}); | |
final int count; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(automaticallyImplyLeading: true), | |
body: Center( | |
child: Text('$count'), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment