当我单击PageView中的按钮时,如何进入页面?

时间:2019-10-03 12:50:54

标签: flutter dart

在Page.dart中制作了一个由6个页面组成的PageView。每个页面都有一个文本小部件,每个页面都包含一个动物名:依次为老虎,狮子,猴子,马,鸡和狗。我在main.dart中做了3个按钮。我只希望当我按下第一个按钮时,我进入页面[0](老虎页面),当我按下第二个按钮时,我进入页面[2](猴子页面),当我按下第三按钮时,我进入了到第[4]页(鸡页面)。但是我完全不知道。我认为必须有某种方法可以解决此问题,但我找不到它。

我搜索了它,并在此网站中对其进行了搜索,但找不到。

import 'package:flutter/material.dart';

List<String> animal = ['tiger', 'lion', 'monkey', 'horse', 'chicken', 'dog'];

class Chapter extends StatefulWidget {
  @override
  _ChapterState createState() => _ChapterState();
}

class _ChapterState extends State<Chapter> {
  // int currentPage;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Chapter Page'),
        ),
        body: PageView.builder(
          itemCount: animal.length,
          itemBuilder: (context, index) {
            return Center(child: Text('${animal[index]} page'));
          },
        ));
  }
}

一些参考文献会很好。 预先谢谢你。

3 个答案:

答案 0 :(得分:1)

您可以使用页面控制器Page Controller实现此目的。检查下面的代码,我在点击页面3上添加了一个导航按钮

Main.dart类

import 'package:flutter/material.dart';
import 'package:flutter_demo_app/chapter_demo.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);

  @override
  _HomeScreenState createState() {
    return _HomeScreenState();
  }
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
  }

  @override    
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Page View Demo'),
      ),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                gotoSelectedPage(1);
              },
              child: Text('GO TO Page 1'),
            ),
            RaisedButton(
              onPressed: () {
                gotoSelectedPage(2);
              },
              child: Text('GO TO Page 2'),
            ),
            RaisedButton(
              onPressed: () {
                gotoSelectedPage(3);
              },
              child: Text('GO TO Page 3'),
            ),
            RaisedButton(
              onPressed: () {
                gotoSelectedPage(4);
              },
              child: Text('GO TO Page 4'),
            )
          ],
        ),
      ),
    );
  }

  gotoSelectedPage(int selectedPage) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Chapter(selectedPage),
      ),
    );
  }
}

Chapter.dart类

import 'package:flutter/material.dart';

List<String> animal = ['tiger', 'lion', 'monkey', 'horse', 'chicken', 'dog'];

class Chapter extends StatefulWidget {
final initialPage;

  Chapter(this.initialPage);
      @override
      _ChapterState createState() => _ChapterState();
    }

class _ChapterState extends State<Chapter> {
  PageController _pageController;

  @override
  void initState() {
    _pageController = PageController(initialPage: widget.initialPage);
    super.initState();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chapter Page'),
      ),
      body: PageView.builder(
    controller: _pageController,
    itemCount: animal.length,
    itemBuilder: (context, index) {
      return Center(
        child: Text('${animal[index]} page'),
      );
    },
  ),
    );
  }
}

答案 1 :(得分:1)

//Guys use both custumpage view and tab view 
//hopefully it will give you help InshaAllah
//other wise whatsapp 03463480361 for hepl in code
//thanks   

 import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    TabController _controller;
    const activeColor = Colors.red;
    const InactiveColor = Colors.grey;
    
    enum Buttons {
      Fix,
      Custom,
    }
    const ktextHeadlineStyle =
        TextStyle(fontWeight: FontWeight.bold, fontSize: 20, color: Colors.red);
    const ktextSubHeadlineStyle =
        TextStyle(fontSize: 15, fontWeight: FontWeight.bold, color: Colors.red);
    const kTapTextStyle = TextStyle(color: Colors.white, fontSize: 20);
    const kButtonTextStyle =
        TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold);
    
    class SubCategoryScreen extends StatefulWidget {
      @override
      _SubCategoryScreenState createState() => _SubCategoryScreenState();
    }
    
    class _SubCategoryScreenState extends State<SubCategoryScreen>
        with SingleTickerProviderStateMixin {
      PageController pageController = PageController();
      //TabController _controller=TabController(length: length, vsync: vsync);
    
      @override
      void initState() {
        super.initState();
        pageController = PageController(initialPage: 0);
        _controller = new TabController(length: 3, vsync: this);
      }
    
      dispose() {
        pageController.dispose();
        _controller.dispose();
        super.dispose();
      }
    
      Color fixedJobColor = activeColor;
      Color customJobColor = InactiveColor;
    
      Buttons selectedColor;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: ListView(
            children: [
              Container(
                height: 200,
                child: Stack(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(33),
                              bottomRight: Radius.circular(33)),
                          image: DecorationImage(
                              image: NetworkImage('https://st.depositphotos.com/1000291/3041/i/950/depositphotos_30414567-stock-photo-adult-electrician-engineer-worker.jpg'),
                              fit: BoxFit.fill)),
                    ),
                    Align(
                      alignment: Alignment.bottomCenter,
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 30),
                        child: Container(
                          child: SizedBox(
                            height: 40,
                            child: TextField(
                              decoration: new InputDecoration(
                                filled: true,
                                fillColor: Colors.white,
                                prefixIcon: Icon(Icons.search),
                                focusedBorder: OutlineInputBorder(
                                  //  borderRadius: BorderRadius.circular(33),
                                  borderSide: BorderSide(
                                      color: Colors.greenAccent, width: 5.0),
                                ),
                                enabledBorder: OutlineInputBorder(
                                  borderSide:
                                      BorderSide(color: Colors.black, width: 1),
                                ),
                                hintText: 'Search in categories',
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    Row(
                      children: [
                        IconButton(
                            icon: Icon(
                              Icons.arrow_back_ios,
                              color: Colors.white,
                            ),
                            onPressed: () {}),
                        SizedBox(
                          width: 80,
                        ),
                        Text(
                          "Electrician",
                          style: ktextHeadlineStyle.copyWith(color: Colors.white),
                        ),
                      ],
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 30,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  MaterialButton(
                    splashColor: Colors.red,
                    onPressed: () {
                      setState(() {
                        selectedColor = Buttons.Fix;
                      });
                      pageController.animateToPage(0,
                          duration: Duration(seconds: 1),
                          curve: Curves.easeOutSine);
                    },
                    child: Text('Fixed Jobs', style: kButtonTextStyle),
                    color: selectedColor==Buttons.Fix?activeColor:InactiveColor,
                  ),
                  MaterialButton(
                    splashColor: Colors.red,
                    onPressed: () {
                      setState(() {
                        selectedColor = Buttons.Custom;
                      });
                      pageController.animateToPage(1,
                          duration: Duration(seconds: 1),
                          curve: Curves.easeOutSine);
                    },
                    child: Text('Custom Jobs', style: kButtonTextStyle),
                    color:selectedColor==Buttons.Custom?activeColor:InactiveColor,
                  ),
                ],
              ),
              Column(
                children: [
                  Container(
                    height: MediaQuery.of(context).size.height * .4,
                    child: PageView(
                      controller: pageController,
                      onPageChanged: (index) {},
                      children: [
                        FixedJobs(),
                        CustomJobs(),
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        );
      }
    }
    
    BoxDecoration kBoxDecoration() {
      return BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(10)),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.5),
            spreadRadius: 5,
            blurRadius: 7,
            offset: Offset(0, 3), // changes position of shadow
          ),
        ],
      );
    }
    
    class FixedJobs extends StatefulWidget {
      @override
      _FixedJobsState createState() => _FixedJobsState();
    }
    
    class _FixedJobsState extends State<FixedJobs> {
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 20),
          child: ListView(
            scrollDirection: Axis.vertical,
            children: [
              Container(
                decoration: kBoxDecoration(),
                child: MaterialButton(
                  splashColor: Colors.red,
                  onPressed: () {
                    // pageController.animateToPage(0,
                    //     duration: Duration(seconds: 1),
                    //     curve: Curves.easeOutSine);
                  },
                  child: TabBar(
                    controller: _controller,
                    isScrollable: true,
                              indicatorSize: TabBarIndicatorSize.label,
                              indicatorColor: Colors.yellow,
                              indicatorWeight: 10.0,
                              indicator: BoxDecoration(color: Colors.red),
                    tabs: [
                    Text('Taps', style: kButtonTextStyle.copyWith(color: Colors.red)),
                    Text('Taps', style: kButtonTextStyle.copyWith(color: Colors.red)),
                    Text('Taps', style: kButtonTextStyle.copyWith(color: Colors.red)),
                    ],
                  ),
                  // child: Text('Taps',
                  //     style: kButtonTextStyle.copyWith(color: Colors.red)),
                  // color: Colors.yellow,
                ),
              ),
              Container(
                height: 300,
                child: TabBarView(
                  controller: _controller,
                    children:[
                      new Container(
                        color: Colors.yellow,
                      ),
                      new Container(
                        color: Colors.orange,
                      ),
                      new Container(
                        color: Colors.lightGreen,
                      ),
    
                    ],
                ),
              ),
              // Column(
              //   children: [
              //     Padding(
              //       padding: const EdgeInsets.symmetric(vertical: 10),
              //       child: Container(
              //         color: Color(0XFF252C41),
              //         child: TabBar(
              //           isScrollable: true,
              //           // indicatorSize: TabBarIndicatorSize.label,
              //           indicatorColor: Colors.yellow,
              //           indicatorWeight: 10.0,
              //           indicator: BoxDecoration(color: Colors.red),
              //           controller: _controller,
              //           tabs: [
              //             new Tab(child: Text("Bio", style: kTapTextStyle)),
              //             new Tab(
              //               child: Text("Training Location",
              //                   style: kTapTextStyle),
              //             ),
              //             new Tab(
              //                 child: Text("Reviews", style: kTapTextStyle)),
              //             new Tab(
              //                 child: Text("Reviewed", style: kTapTextStyle)),
              //           ],
              //         ),
              //       ),
              //     ),
              //     Container(
              //       height: MediaQuery.of(context).size.height + 300,
              //       width: MediaQuery.of(context).size.width,
              //       child: TabBarView(
              //         controller: _controller,
              //         children: [
              //           new Card(
              //               child: Column(
              //                 crossAxisAlignment: CrossAxisAlignment.start,
              //                 children: [
              //                   Text(
              //                     "James White",
              //                     style: ktextHeadlineStyle,
              //                   ),
              //                   Padding(
              //                     padding:
              //                     const EdgeInsets.symmetric(vertical: 20),
              //                     child: Text(
              //                       "Football",
              //                       style: ktextHeadlineStyle,
              //                     ),
              //                   ),
              //                   Text(
              //                     "When developing an application, I had the need for a TabBar with buttons as Tabs. After searching around, I didn’t find anything; the default TabBar widget didn’t have that option, therefore, I decided to make it myself.",
              //                     style: ktextSubHeadlineStyle,
              //                   ),
              //                   SizedBox(
              //                     height: 80,
              //                   ),
              //                   // Center(
              //                   //     child: EditBottons(
              //                   //       onPressed: () {},
              //                   //     )),
              //                 ],
              //               )),
              //           new Card(
              //               child: Padding(
              //                 padding: const EdgeInsets.symmetric(horizontal: 15),
              //                 child: Column(
              //                   crossAxisAlignment: CrossAxisAlignment.start,
              //                   children: [
              //                     Padding(
              //                       padding:
              //                       const EdgeInsets.symmetric(vertical: 10),
              //                       child: Text("Enter Your Name"),
              //                     ),
              //                     SizedBox(
              //                       height: 30,
              //                       child: TextField(
              //                         decoration: InputDecoration(
              //                             filled: true,
              //                             fillColor: Color(0XFFE0E0E0),
              //                             border: OutlineInputBorder(
              //                               borderRadius:
              //                               BorderRadius.circular(33),
              //                             )),
              //                       ),
              //                     ),
              //                     //SizedBox(height: 20,),
              //                     // Row(
              //                     //   children: [
              //                     //     TextFieldBottumLine(
              //                     //       onChanged: null,
              //                     //       name: "Street Address",
              //                     //     ),
              //                     //     TextFieldBottumLine(
              //                     //       onChanged: null,
              //                     //       name: "City",
              //                     //     ),
              //                     //     TextFieldBottumLine(
              //                     //       onChanged: null,
              //                     //       name: "State",
              //                     //     ),
              //                     //   ],
              //                     // ),
              //                     // Row(
              //                     //   children: [
              //                     //     TextFieldBottumLine(
              //                     //       onChanged: null,
              //                     //       name: "Zip Code",
              //                     //     ),
              //                     //     TextFieldBottumLine(
              //                     //       onChanged: null,
              //                     //       name: "City",
              //                     //     ),
              //                     //   ],
              //                     // ),
              //                     SizedBox(
              //                       height: 30,
              //                     ),
              //                     Padding(
              //                       padding: const EdgeInsets.only(left: 240),
              //                       child: Container(
              //                         decoration: BoxDecoration(
              //                           color: Colors.red,
              //                           borderRadius: BorderRadius.circular(22),
              //                         ),
              //                         height: 40,
              //                         width: 100,
              //                         child: MaterialButton(
              //                           onPressed: () {},
              //                           child: Text(
              //                             "Save",
              //                             style: ktextSubHeadlineStyle.copyWith(
              //                                 color: Colors.white),
              //                           ),
              //                         ),
              //                       ),
              //                     ),
              //                     SizedBox(
              //                       height: 20,
              //                     ),
              //                     Text(
              //                       'Location',
              //                       style: ktextSubHeadlineStyle,
              //                     ),
              //                     SizedBox(
              //                       height: 20,
              //                     ),
              //                     RaisedButton(
              //                       shape: RoundedRectangleBorder(
              //                           borderRadius: BorderRadius.circular(18.0),
              //                           side: BorderSide.none),
              //                       onPressed: () {},
              //                       color: Colors.grey,
              //                       elevation: 20,
              //                       textColor: Colors.white,
              //                       child: Row(
              //                         mainAxisAlignment:
              //                         MainAxisAlignment.spaceBetween,
              //                         children: <Widget>[
              //                           Icon(Icons.location_on),
              //                           Text(
              //                             'New York NY United States',
              //                             style: ktextSubHeadlineStyle.copyWith(
              //                                 color: Colors.black),
              //                           ),
              //                           Icon(Icons.delete)
              //                         ],
              //                       ),
              //                     ),
              //                     RaisedButton(
              //                       shape: RoundedRectangleBorder(
              //                           borderRadius: BorderRadius.circular(18.0),
              //                           side: BorderSide.none),
              //                       onPressed: () {},
              //                       color: Colors.grey,
              //                       elevation: 20,
              //                       textColor: Colors.white,
              //                       child: Row(
              //                         mainAxisAlignment:
              //                         MainAxisAlignment.spaceBetween,
              //                         children: <Widget>[
              //                           Icon(Icons.location_on),
              //                           Text(
              //                             'New York NY United States',
              //                             style: ktextSubHeadlineStyle.copyWith(
              //                                 color: Colors.black),
              //                           ),
              //                           Icon(Icons.delete)
              //                         ],
              //                       ),
              //                     ),
              //                     SizedBox(
              //                       height: 50,
              //                     ),
              //                     // Center(
              //                     //     child: EditBottons(
              //                     //       onPressed: () {},
              //                     //     ))
              //                   ],
              //                 ),
              //               )),
              //           new Card(
              //             child: ListView(children: [
              //               ListTile(
              //                 leading: Container(
              //                   height: 50,
              //                   width: 50,
              //                   decoration: BoxDecoration(
              //                       image: DecorationImage(
              //                           image: AssetImage('assets/pic.jpg'),
              //                           fit: BoxFit.fill)),
              //                 ),
              //                 title: Text("Jane Pin"),
              //                 subtitle: Text(
              //                     'ajfideifheihafdifdf difidhafa dfd af d faf difidhafdhf d f hfiahfdhif  '),
              //               ),
              //               ListTile(
              //                 leading: Container(
              //                   height: 50,
              //                   width: 50,
              //                   decoration: BoxDecoration(
              //                       image: DecorationImage(
              //                           image: AssetImage('assets/pic.jpg'),
              //                           fit: BoxFit.fill)),
              //                 ),
              //                 title: Text("Jane Pin"),
              //                 subtitle: Text(
              //                     'ajfideifheihafdifdf difidhafa dfd af d faf difidhafdhf d f hfiahfdhif  '),
              //               ),
              //               ListTile(
              //                 leading: Container(
              //                   height: 50,
              //                   width: 50,
              //                   decoration: BoxDecoration(
              //                       image: DecorationImage(
              //                           image: AssetImage('assets/pic.jpg'),
              //                           fit: BoxFit.fill)),
              //                 ),
              //                 title: Text("Jane Pin"),
              //                 subtitle: Text(
              //                     'ajfideifheihafdifdf difidhafa dfd af d faf difidhafdhf d f hfiahfdhif  '),
              //               ),
              //               ListTile(
              //                 leading: Container(
              //                   height: 50,
              //                   width: 50,
              //                   decoration: BoxDecoration(
              //                       image: DecorationImage(
              //                           image: AssetImage('assets/pic.jpg'),
              //                           fit: BoxFit.fill)),
              //                 ),
              //                 title: Text("Jane Pin"),
              //                 subtitle: Text(
              //                     'ajfideifheihafdifdf difidhafa dfd af d faf difidhafdhf d f hfiahfdhif  '),
              //               ),
              //             ]),
              //           ),
              //           new Card(
              //             child: new ListTile(
              //               leading: const Icon(Icons.location_on),
              //               title: new Text(
              //                   'Latitude: 48.09342\nLongitude: 11.23403'),
              //               trailing: new IconButton(
              //                   icon: const Icon(Icons.my_location),
              //                   onPressed: () {}),
              //             ),
              //           ),
              //         ],
              //       ),
              //     )
              //   ],
              // ),
            ],
          ),
        );
      }
    }
    
    class CustomJobs extends StatefulWidget {
      @override
      _CustomJobsState createState() => _CustomJobsState();
    }
    
    class _CustomJobsState extends State<CustomJobs> {
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.all(30),
          child: Container(
            height: MediaQuery.of(context).size.height,
            decoration: kBoxDecoration(),
            child: Column(
              children: [
                Expanded(
                  child: Row(
                    // mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Expanded(
                          child: Icon(
                        Icons.account_circle,
                        size: 40,
                      )),
                      Expanded(
                        child: Padding(
                          padding: const EdgeInsets.only(right: 20),
                          child: Text(
                              "Please describe the work thatyou need. Our professionalwill contact you"),
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 20),
                  child: Container(
                      child: Material(
                    // elevation: 5,
                    child: Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 30.0, vertical: 10),
                      child: SizedBox(
                        width: 350.0,
                        height: 100.0,
                        child: TextField(
                          //   controller: notification,
                          style: TextStyle(fontSize: 20.0),
                          maxLines: 10,
                          decoration: InputDecoration(
                            fillColor: Colors.white,
                            filled: true,
                            border: OutlineInputBorder(
                                borderSide: BorderSide(width: 2)),
                            hintText: 'Enter Your Text',
                          ),
                        ),
                      ),
                    ),
                  )),
                ),
              ],
            ),
          ),
        );
      }
    }

答案 2 :(得分:1)

我认为您正在寻找这个:

_pageController.nextPage(
     duration: Duration(milliseconds: 1000),
     curve: Curves.easeIn
);