initialRoute

initialRoute

MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})

açıklama

varsayılan ilk açılacak ana sayfayı belirler.

konum

MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})

parametreler

                 //Rout...............................................................................................
                                   initialRoute:   "/",   // varsayılan ilk açılacak ana sayfa ,
                                         routes:   {
                                                      '/sayfabir':  (context) => new SayafaBir(),
                                                      '/sayfaiki' : (context) => new SayafaIki(),
                                                      '/sayfauc' : (context) => new SayafaUc(),
                                                      '/sayfadort' : (context) => new SayafaDort()
                                                    },

örnek uygulama

color-selector-orn1
named-route-orn2
 
dosya adi:main.dart

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_navigator_example/color-selector.dart';
import 'package:flutter_navigator_example/named-route.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Map<String, WidgetBuilder> staticRoutes = {
    '/': (context) => MyHomePage(title: 'Flutter Navigator Example'),
  };

  @override
  Widget build(BuildContext context) {
    Map<String, WidgetBuilder> routes = Map();
    routes.addAll(staticRoutes);
    routes.addAll(_generateRoutes());
    return MaterialApp(
      title: 'Flutter Navigator Example',
      theme: ThemeData(
        primarySwatch: Colors.indigo,
      ),
      initialRoute: '/',
      routes: routes,
    );
  }

  Map<String, WidgetBuilder> _generateRoutes() {
    int total = 50;
    return Map.fromIterable(
        List.generate(total, (value) => [value, _generateRoute(value, total)]),
        key: (pair) => '/random/${pair[0]}',
        value: (pair) => pair[1]);
  }

  WidgetBuilder _generateRoute(int number, int total) {
    return (context) => NamedRouteWidget(number, total);
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              child: Text('Show simple route'),
              onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => Scaffold(
                        appBar: AppBar(
                          title: Text('Simple route'),
                        ),
                        body: Center(
                          child: RaisedButton(
                            onPressed: () => Navigator.pop(context),
                            child: Text('Return to main screen'),
                          ),
                        ),
                      ))),
            ),
            RaisedButton(
              child: Text('Color selector example'),
              onPressed: () => Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => ColorSelectorPage())),
            ),
            RaisedButton(
              child: Text('Random route'),
              onPressed: () => Navigator.of(context).pushNamed('/random/0'),
            )
          ],
        ),
      ),
    );
  }
}
 
dosya adi: color-selector.dart

import 'package:flutter/material.dart';

class ColorSelectorPage extends StatefulWidget {
  @override
  _ColorSelectState createState() {
    return _ColorSelectState();
  }
}

class _ColorSelectState extends State {
  Color color;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Color selector example'),
      ),
      body: Container(
          color: color,
          child: Center(
            child: RaisedButton(
              child: Text('Select background color'),
              onPressed: _showColorSelector,
            ),
          )),
    );
  }

  _showColorSelector() async {
    color = await Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => ColorSelector(color)));
  }
}

class ColorSelector extends StatelessWidget {
  final List colors = [
    Colors.red,
    Colors.green,
    Colors.blue,
    Colors.orange,
  ];

  final Color selectedColor;

  ColorSelector(this.selectedColor);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Select a color'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              children: [
                Text('Selected color: $selectedColor'),
                Column(
                  children: colors
                      .map((color) => RaisedButton(
                            child: Text(color.toString()),
                            textColor: color,
                            onPressed: () => Navigator.of(context).pop(color),
                          ))
                      .toList(),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

 
dosya adi:named-route.dart

import 'dart:math';

import 'package:flutter/material.dart';

class NamedRouteWidget extends StatelessWidget {
  final int routeNumber;
  final int numberOfRoutes;
  final Random random = Random();

  NamedRouteWidget(this.routeNumber, this.numberOfRoutes);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Random route $routeNumber'),
      ),
      body: Center(
        child: RaisedButton(
            child: Text('Go to a random route'),
            onPressed: () => Navigator.of(context)
                .pushNamed('/random/${random.nextInt(numberOfRoutes)}')),
      ),
    );
  }
}

Bir cevap yazın

KAPAT
%d blogcu bunu beğendi: