Skip to content

Instantly share code, notes, and snippets.

@axl0d
Created February 24, 2021 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axl0d/841e3da1258dc6edc9d71a0168cb6782 to your computer and use it in GitHub Desktop.
Save axl0d/841e3da1258dc6edc9d71a0168cb6782 to your computer and use it in GitHub Desktop.
Answer: Como estructurar Switch Case en Dart?, a dartpad gist that have a method to navigate between pages written from textfield
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
String _name = '';
@override
Widget build(BuildContext context) {
return Center(
child: Padding(
padding: const EdgeInsets.all(16),
child: TextField(
decoration: InputDecoration(hintText: 'Ingrese nombre'),
onChanged: (value) {
setState(() {
_name = value;
print(value);
});
},
onEditingComplete: _navigateToPage,
),
),
);
}
_navigateToPage() {
Widget destination;
switch (_name) {
case 'asesor':
destination = AsesorPage();
break;
case 'asesorz':
default:
destination = AsesorzPage();
}
Navigator.push(
context,
MaterialPageRoute(builder: (_) => destination),
);
}
}
class AsesorPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text('asesor'),
),
);
}
}
class AsesorzPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text('asesorz'),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment