// 1. Separate widget : this example MyHome() out of MaterialApp()
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen()
);
}
}
// Separate HomeScreen()
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Text("Click Me"),
onPressed: () => Navigator.pushNamed(context, "/"),
),
);
}
}
// 2. Use Builder:
// Wrap with Builder if use same in MaterialApp()
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder( // Builder:
builder: (context) => Center(
child: ElevatedButton(
child: Text("Click Me"),
onPressed: () => Navigator.pushNamed(context, "/"),
),
),
),
);
}
}
// 1. Separate widget : this example MyHome() out of MaterialApp()
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen()
);
}
}
// Separate HomeScreen()
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Text("Click Me"),
onPressed: () => Navigator.pushNamed(context, "/"),
),
);
}
}
// 2. Use Builder:
// Wrap with Builder if use same in MaterialApp()
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder( // Builder:
builder: (context) => Center(
child: ElevatedButton(
child: Text("Click Me"),
onPressed: () => Navigator.pushNamed(context, "/"),
),
),
),
);
}
}